Search code examples
jsonapache-nifijolt

Convert the array values into json object using Jolt Transform in Apache NiFi


I want to convert the array values into the Individual JSON object using Jolt transform NiFi.

Input:

{
  "headers": {
    "query": "NA",
    "param": "false"
  },
  "data": [
    {
      "SEQ_NUM": [
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        162
      ]
    },
    {
      "SEQ_NUM": [
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        162
      ]
    }
  ]
}

Jolt Spec:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "": {
          "SEQ_NUM": {
            "": {
              "@": "[&1].SEQ_NUM"
            }
          }
        }
      }
    }
  }
]

Solution

  • Your jolt spec is wrong. You should add * to empty keys in your jolt. So you can use this spec:

    [
      {
        "operation": "shift",
        "spec": {
          "data": {
            "*": {
              "SEQ_NUM": {
                "*": {
                  "@": "[&1].SEQ_NUM"
                }
              }
            }
          }
        }
      }
    ]
    

    enter image description here

    If you want to prevent duplicate values in the output, You can use this spec:

    [
      {
        "operation": "shift",
        "spec": {
          "data": {
            "0": {
              "SEQ_NUM": {
                "*": {
                  "@": "[&1].SEQ_NUM"
                }
              }
            }
          }
        }
      }
    ]
    

    enter image description here