Skip to main content
rustoherox
Gold ⭐️
May 25, 2022
Solved

Make flat json array from nested one via JOLT

Hi everyone!

I'm trying to flat array and it view as simple case, but I can't reach the goal.

Here is simple json input:

{
"MainArray": {
"mattr1": "some_value1",
"mattr2": "some_value2",
"Subarray": {
"sattr1": 1,
"sattr2": 2,
"sattr3": 3
}
}
}

Then I would like to convert it to array of arrays [key, value],
Here is Desired Output:

{
"ResultArray" : [
[ "mattr1", "some_value1" ],
[ "mattr2", "some_value2" ],
[ "sattr1", 1 ],
[ "sattr2", 2 ],
[ "sattr1", 3 ]
]
}

 Here is my spec:

[
{
"operation": "shift",
"spec": {
"MainArray": {
"*": {
"$": "ResultArray[#2][0]",
"@": "ResultArray[#2][1]"
},
"Subarray": {
"*": {
"$": "ResultArray[#3][#2][0]",
"@": "ResultArray[#3][#2][1]"
}
}
}
}
}
]

 And I don't understand how to fix problem when Subarrays is inside extra array in my result:

{
"ResultArray" : [
[ "mattr1", "some_value1" ],
[ "mattr2", "some_value2" ],
[
[ "sattr1", 1 ],
[ "sattr2", 2 ],
[ "sattr3", 3 ]
]
]
}

 Any advise, please.

rustoherox
Gold ⭐️
May 27, 2022

Actually it is not possible to solve this case in one shift - here is discussion and workaround with append subarray to mainarray.

3 replies

SAMSAL
Platinum ⭐️⭐️⭐️
May 25, 2022

I'm not sure why your spec is producing this result, but to resolve it you can do the transformation on two shift operations: First to flatting json and move the subarray level to the root level and second to move the result and store it into an Array. Here is the spec I tried and it worked:

 

[
{
"operation": "shift",
"spec": {
"MainArray": {
"*": "ResultArray.&",
"Subarray": {
"*": "ResultArray.&"
}
}
}
},
{
"operation": "shift",
"spec": {
"ResultArray": {
"*": {
"$": "ResultArray[#2][0]",
"@": "ResultArray[#2][1]"
}
}
}
}
]

Translate
rustoherox
Gold ⭐️
May 25, 2022

@SAMSAL Thank for your variant, but it is not actually desired, because Subarray may be another structure of jolt rules inside for example like this

[
{
"operation": "shift",
"spec": {
"MainArray": {
"*": {
"$": "ResultArray[#2][0]",
"@": "ResultArray[#2][1]"
},
"Subarray": {
"*": {
"$": "ResultArray[#3][#2][0]",
"@": "ResultArray[#3][#2][1]",
"#some_value": "ResultArray[#3][#2][3]"
}
}
}
}
}
]

Btw it's my fault cause of not actually correct example (same rules for main and sub arrays)

Translate
rustoherox
rustoheroxAuthorAnswer
Gold ⭐️
May 27, 2022

Actually it is not possible to solve this case in one shift - here is discussion and workaround with append subarray to mainarray.

Translate