Below is my JSON
[
{
"id": "0",
"bucket_attr1": "test1",
"buckets": ["abc", "def", "mhf"]
},
{
"id": "1",
"bucket_attr1": "test2",
"buckets": ["abc", "ghi"]
}
]
I want to copy id and bucket_attr1 into each map in the buckets array, such that output is as follows
[
{
"bucket_id": "abc",
"bucket_attr1": "test1",
"id": "0"
},
{
"bucket_id": "def",
"bucket_attr1": "test1",
"id": "0"
},
{
"bucket_id": "mhf",
"bucket_attr1": "test1",
"id": "0"
},
{
"bucket_id": "abc",
"bucket_attr2": "test2",
"id": "1"
},
{
"bucket_id": "ghi",
"bucket_attr2": "test2",
"id": "1"
}
]
I tried several ways including what is mentioned here after slight modification, but it did not work. Jolt Transformation to move an attribute in each element of the array
You can use the following shift transformation specs :
[
{// separate by indexes from two different levels
"operation": "shift",
"spec": {
"*": {
"buckets": {
"*": {
"@": "&3_&1.bucket_id",
"@2,id": "&3_&1.id",
"@2,bucket_attr1": "&3_&1.bucket_attr1"
}
}
}
}
},
{// get rid of the separator keys of the objects
"operation": "shift",
"spec": {
"*": "[]"
}
}
]
or we can make it more dynamic such as
[
{ // separate by indexes from two different levels
"operation": "shift",
"spec": {
"*": {
"*": "&1.others.&", // nest other elements within a common object to be used within the upcoming spec
"buckets": {
"*": {
"@": "&3.&2[&1].bucket_id"//rename as desired
}
}
}
}
},
{ //get three independent arrays
"operation": "shift",
"spec": {
"*": {
"buckets": {
"*": {
"@2,others": { "*": "[#6].&" },
"*": "[#5].&"
}
}
}
}
},
{ //dissipate each array elements to different individual objects
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "[&].&1"
}
}
}
}
]
the demo for the second one on the site https://jolt-demo.appspot.com/ is :