Search code examples
arraysjsonapache-nifijolt

Jolt transform to flatten a array in Nifi


I have the following input JSON :

{
  "a":[1,2,3,[4,5,6], [7], [8,9], 10, 11]
}

And I would like the output to be :

{
  "a":[1,2,3,4,5,6,7,8,9,10,11]
}

What can be jolt transform for this. Thanks!


Solution

  • You can apply consecutive shift transformations starting by seperating the elements to two arrays, one of which has sub-arrays, and the other has integers, namely x and y arrays . Then combine them while removing the null values, and convert stringified elements back to integers such as

    [
      {
        "operation": "shift",
        "spec": {
          "a": {
            "*": {
              "@": "x.[@1]",
              "*": "y.[]"
            }
          }
        }
     },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "$1": "@1"
            }
          }
        }
     },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "$": "@(0)"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "@": "a"
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": "=toInteger"
        }
      }
    ]