jsonapache-nifijolt

Convert an array of 2 arrays (numeric) into a single array


I have to convert an array of 2 arrays (numeric) into a single array with each element having one value from both arrays using JOLT Transform in Nifi.

Input:

{
  "md": {
    "fn": "abcd"
  },
  "content": {
    "idc": "0",
    "lsr": "3",
    "lsc": "A",
    "array Of Arrays": [
      {
        "name": "step_1",
        "row": "2",
        "column": "A",
        "gph": {
          "a values": [
            0.23,
            1.39
          ],
          "t values": [
            0.3,
            1.9
          ]
        }
      },
      {
        "name": "step_2",
        "row": "3",
        "column": "A",
        "gph": {
          "a values": [
            0.56,
            2.49
          ],
          "t values": [
            1.4,
            2.8
          ]
        }
      }
    ]
  }
}

Current JOLT Spec:

[
  {
    "operation": "shift",
    "spec": {
      "md": {
        "fn": "fn"
      },
      "content": {
        "idc": "idc",
        "lsr": "lsr",
        "lsc": "lsc",
        "array Of Arrays": {
          "*": {
            "name": "name[]",
            "s": "s[]",
            "p": "p[]",
            "gph": "&[]"
          }
        }
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "sop": "=concat(@(2,lsr),@(2,lsc))"
    }
  }
]

Current Output:

{
  "fn" : "abcd",
  "idc" : "0",
  "lsr" : "3",
  "lsc" : "A",
  "name" : [ "step_1", "step_2" ],
  "s" : [ "2", "3" ],
  "p" : [ "A", "A" ],
  "gph" : [ {
    "a values" : [ 0.23, 1.39 ],
    "t values" : [ 0.3, 1.9 ]
  }, {
    "a values" : [ 0.56, 2.49 ],
    "t values" : [ 1.4, 2.8 ]
  } ],
  "sop" : "3A"
}

Expected Output:

{
  "fn" : "abcd",
  "idc" : "0",
  "array" : [
    {"name": "step_1", "sp": "2A", "a_value": 0.23, "t_value": 0.3},
    {"name": "step_1", "sp": "2A", "a_value": 1.39, "t_value": 1.9},
    {"name": "step_2", "sp": "3A", "a_value": 0.56, "t_value": 1.4},
    {"name": "step_2", "sp": "3A", "a_value": 2.49, "t_value": 2.8}
  ],
  "sop" : "3A"
}

Solution

  • You can use the following transformation specs

    [
      {
        "operation": "shift",
        "spec": {
          "@md.fn": "fn",
          "content": {
            "idc": "&",
            "ls*": "sop", // rename all attributes start with ls as "sop"
            "array Of Arrays": {
              "*": {
                "gph": {
                  "* *s": {
                    "*": {
                      "@3,name": "&4_&1.name",
                      "@3,row": "&4_&1.row",
                      "@3,column": "&4_&1.column",
                      "@": "&4_&1.&(2,1)_&(2,2)"
                    }
                  }
                }
              }
            }
          }
        }
      },
      { //get rid of repeating components which were generated from outer attributes
        "operation": "modify-overwrite-beta",
        "spec": {
          "*_*": {
            "*": "=firstElement"
          },
          "sop": "=join('',@(1,&))" //combine "lsr" & "lsc" attributes
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*_*": {
            "sp": "=concat(@(1,row),@(1,column))" //combine "row" & "column" attributes
          }
        }
      },
      { //get rid of the residual attributes
        "operation": "remove",
        "spec": {
          "*_*": {
            "row|column": ""
          }
        }
      },
      { //generate "array"
        "operation": "shift",
        "spec": {
          "*": "&",
          "*_*": "array[]"
        }
      }
    ]