Search code examples
jsonapache-nifijolt

Flatten the JSON which has comma's in the input value


I am facing a problem, transforming a very complex JSON which has commas in the input values. I need to separate the comma separated values into different records using jolt transformation. Input and output detail is given below.

Input :

{
  "DeptID": "339283",
  "EmpID": "280908,280909",
  "EmpCode": "123,31424",
  "Manager":"James,Thiyags"
}

Output Expected :

[
  {
    "DeptID": "339283",
    "EmpID": "280908",
    "EmpCode": "123",
    "Manager":"James"
  },
  {
    "DeptID": "339283",
    "EmpID": "280909",
    "EmpCode": "31424",
    "Manager":"Thiyags"
  }
]

Pls help Can anyone who is a jolt expert, help me get the desired output. I think i m stuck in the last step


Solution

  • You can split by commas within the modify transformation spec such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "EmpID": "=split(',',@(1,&))"
        }
      },
      { // loop through the "EmpID" array
        "operation": "shift",
        "spec": {
          "EmpID": {
            "*": { // indexes of the array
              "@": "[#2].&2", // &2 replicates the key name "EmpID"
              "@2,DeptID": "[#2].DeptID"
            }
          }
        }
      }
    ]
    

    Edit : You can use the following spec based on your last edit

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "Emp*": "=split(',',@(1,&))"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "Emp*": {
            "*": {
              "@": "[#2].&2",
              "@2,DeptID": "[#2].DeptID"
            }
          }
        }
      },
      {
        "operation": "cardinality",
        "spec": {
          "*": {
            "*": "ONE"
          }
        }
      }
    ]