Split array inside JSON with JOLT

Viewed 146

I have a JSON look like:

[
  {
    "mainId": 12854,
    "subIds": [
      25,
      26,
      27
    ]
  }
]

I want to split values inside subIds to create diffrent rows. Can I get expected result with JOLT?

[
  {
    "mainId": 12854,
    "subId": 25
  },
  {
    "mainId": 12854,
    "subId": 26
  },
  {
    "mainId": 12854,
    "subId": 27
  }
]
1 Answers

You can walk through the indexes of subIds array while grabbing the value of mainId by @(2,mainId) in order to going up the three two levels, and using [&1] as common factor to reach those indexes such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "subIds": {
          "*": {
            "@": "[&1].subId",
            "@(2,mainId)": "[&1].mainId"
          }
        }
      }
    }
  }
]

enter image description here

Related