I’m looking for flattening nested JSON file into SQL ready format.
JSON file’s content:
{
"ProductLine": [
"Product 1",
"Product 2"
],
"Purchase": 364,
"Cancel": [
140,
2
]
}
My current transformation:
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"@": "[#2].&2"
}
}
}
}
]
Desired output:
[
{
"ProductLine": "Product 1",
"Purchase": 364,
"Cancel": 140
},
{
"ProductLine": "Product 2",
"Cancel": 2
}
]
The difficulty is that arrays can change, sometimes "Cancel" can be an array or sometimes "Purchase" block can be nested.
2
Answers
You can use this spec:
If
Purchase
orcancel
be an array or not, this worksFirst, change all values to the array. Now you can loop on the
ProductLine
and get other fields fromPurchase
andCancel
.Update: The following answer has been obtained in collaboration with Barbaros Özhan. Special thanks.
We can pick
Purchase
at a different(outer) level such asthe demo one the site http://jolt-demo.appspot.com/ is
Edit : Considering array indeterminance for the attributes, you can use the following spec alternatively
or another straightforward alternative would be using your original spec after applying cardinality spec such as