Search code examples
jsonapache-nifijolt

How to Build Output JSON Using Jolt Transform in NiFi


I'm facing an issue while trying to construct an output JSON from a joltTransform processor in Apache NiFi. I need to extract values from the idByLine array and match them with values in the orderLine array. Here's a simplified version of my input JSON:

{
  "orderLine": [
    {
      "ID": "1"
    },
    {
      "ID": "2"
    }
  ],
  "idByLine": [
    {
      "idLine": "1",
      "orderId": "AMD123"
    },
    {
      "idLine": "2",
      "orderId": "AMD555"
    }
  ]
}

My goal is to create the following output object:

[
  {
    "idLine": "AMD123"
  },
  {
    "idLine": "AMD555"
  }
]

I'm struggling to devise the correct JOLT specification to achieve this. Could someone please assist me in crafting the appropriate JOLT spec for generating the desired output?


Solution

  • You can try the following spec:

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "ID": {
                "@": "@(0).&1"
              },
              "idLine": {
                "@(1,orderId)": "@(0).&1"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "ID": {
              "@(1,idLine)": "[].idLine"
            }
          }
        }
      }
    ]