Search code examples
jsonapache-nifijolt

Needed Jolt Specification in NiFi Processor to Convert JSON


I am new to using Jolt specifications and need some assistance. I am trying to convert a JSON object using a Jolt specification in an Apache NiFi processor. The input JSON looks like this:

{
  "keys": [
    "CustomerNumber",
    "CustomerName",
    "IsActive"
  ],
  "values": [
    [
      "12345",
      "ABC Corp",
      "1"
    ],
    [
      "67890",
      "XYZ Ltd",
      "0"
    ]
  ]
}

I need to transform this into a key-value pair format, where each object in the values array is converted to a JSON object with the keys as field names. The desired output should look like this:

[
  {
    "CustomerNumber": "12345",
    "IsActive": "1",
    "CustomerName": "ABC Corp"
  },
  {
    "CustomerNumber": "67890",
    "IsActive": "0",
    "CustomerName": "XYZ Ltd"
  }
]

Could someone help me with the correct Jolt specification to achieve this transformation in NiFi? Any guidance or examples would be greatly appreciated.

Thank you!


Solution

  • You can add the following shift transformation into the specificaton part of a JoltTtansformJSON proessor :

    [
      {
        "operation": "shift",
        "spec": {
          "values": {
            "*": {
              "*": "[&1].@(3,keys[&])" //need to traverse 3 layers in order to reach the level of "keys" array
                                       //use the index values of the urrent level by [&] for the innermost match
                                       //use the indexes of the "values" array through [&1]
            }
          }
        }
      }
    ]
    

    the demo on the site https://jolt-demo.appspot.com/ is :

    enter image description here