Search code examples
arraysjsonapache-nifijolt

Jolt tranfrorm nested json array


How can I break and flatten nested JSON with arrays using Jolt transformations

from:

{
  "product": [
    "test1",
    "test2"
  ],
  "Purchase": [
    1,
    2
  ],
  "Renewal": [
    1,
    2
  ]
}

to:

[
  {
    "product": "test1",
    "Purchase": 1,
    "Renewal": 1
  },
  {
    "product": "test2",
    "Purchase": 2,
    "Renewal": 2
  }
]

I want to flatten this array json file in order to fit to sql db format.


Solution

  • You can use a shift transformation spec such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "@": "[#2].&2" 
            }
          }
        }
      }
    ]
    

    where &2 leaf separates values into new sub-arrays by going to levels up to grab their current respective array names, and [#2] re-organizes them to form array of objects.

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

    enter image description here

    Edit : If the input is like the below one as asked in the last comment

    {
      "product": [
        "test- 1",
        "test- 2"
      ],
      "Purchase": [
        2,
        2
      ],
      "Renewal": 1
    }
    

    then you can consider using

    [
      {
        "operation": "shift",
        "spec": {
          "*r*": {
            "*": {
              "@": "[#2].&2",
              "@(2,Renewal)": "[#2].Renewal"
            }
          }
        }
      },
      {
        "operation": "cardinality",
        "spec": {
          "*": {
            "Re*": "ONE"
          }
        }
      }
    ]