4

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
  }
]
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
fujidaon
  • 355
  • 1
  • 6

1 Answers1

2

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": {
      "*": {
        "*s": {
          "*": {
            "@": "[&1].&(2,1)", // &(2,1) : going two levels up the tree to extract the value "subId" from "subIds" by using 1 as the second argument to represent te first asterisk(which might be multiple)
            "@(2,mainId)": "[&1].mainId"
          }
        }
      }
    }
  }
]

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

enter image description here

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
  • 1
    @SarahMesser Lately I've realised what you have meant, and thanks for reminding me that I indeed normally add the link but here seems that I forgot. Btw, you can also check out https://stackoverflow.com/search?tab=newest&q=user%3a5841306%20%5bjolt%5d&searchOn=3 for my answers of `Jolt`. Have a nice study! – Barbaros Özhan Mar 01 '23 at 18:58