Created 05-25-2022 06:21 AM
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.
Created on 05-27-2022 01:29 AM - edited 05-27-2022 01:29 AM
Actually it is not possible to solve this case in one shift - here is discussion and workaround with append subarray to mainarray.
Created on 05-25-2022 08:03 AM - edited 05-25-2022 08:03 AM
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]"
}
}
}
}
]
Created 05-25-2022 09:06 AM
@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)
Created on 05-27-2022 01:29 AM - edited 05-27-2022 01:29 AM
Actually it is not possible to solve this case in one shift - here is discussion and workaround with append subarray to mainarray.