Home > Software design >  Jolt tranfrorm nested json array
Jolt tranfrorm nested json array

Time:10-08

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.

CodePudding user response:

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 enter image description here

  • Related