I need to use Jolt to transform a flat JSON object into an array of JSON objects, where each row in the array corresponds to a unique index number from the original object. The output should have as many rows as there were index numbers in the original object.
Input JSON 1
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters.location": "test",
"batters.batter[0].id": "1001",
"batters.batter[0].type": "Regular",
"batters.batter[1].id": "1002",
"batters.batter[1].type": "Chocolate"
}
Output JSON 1
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters.location": "test",
"batters.batter.id": "1001",
"batters.batter.type": "Regular"
},
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters.location": "test",
"batters.batter.id": "1002",
"batters.batter.type": "Chocolate"
}
]
Another example like
Input JSON 2
{
"[0].id": "0001",
"[0].type": "donut",
"[0].name": "Cake",
"[0].ppu": 0.55,
"[0].batters.location": "test",
"[0].batters.batter[0].id": "1001",
"[0].batters.batter[0].type": "Regular",
"[0].batters.batter[1].id": "1002",
"[0].batters.batter[1].type": "Chocolate",
"[1].id": "0002",
"[1].type": "donut",
"[1].name": "Cake2",
"[1].ppu": 0.75,
"[1].batters.location": "test2",
"[1].batters.batter[0].id": "1002",
"[1].batters.batter[0].type": "Regular",
"[1].batters.batter[1].id": "1003",
"[1].batters.batter[1].type": "Chocolate"
}
Output JSON 2
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters.location": "test",
"batters.batter.id": "1001",
"batters.batter.type": "Regular"
},
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters.location": "test",
"batters.batter.id": "1002",
"batters.batter.type": "Chocolate"
},
{
"id": "0002",
"type": "donut",
"name": "Cake2",
"ppu": 0.75,
"batters.location": "test2",
"batters.batter.id": "1002",
"batters.batter.type": "Regular"
},
{
"id": "0002",
"type": "donut",
"name": "Cake2",
"ppu": 0.75,
"batters.location": "test2",
"batters.batter.id": "1003",
"batters.batter.type": "Chocolate"
}
]
2
Answers
The main idea is to partition by square-bracketed integers in order to be able to loop within the upcoming reformed objects. The solution for the first input is
the similar logic might be applied to the other input with a little bit of tweaks
You can achieve your both desired outputs with this single JOLT spec: