I am facing a problem, transforming flat JSON to the nested JSON using jolt transformation.
Data should be nested based on bucket_id and location keys
Input Json:
[
{
“item_attr1”: “abc”,
“item_attr2”: “123”,
“item_attr3”: “123”,
“item_id”: “1234885”,
“bucket_attr1”: 1919,
“bucket_attr2”: “abc”,
“bucket_attr3”: 1922,
“bucket_attr4”: “abc”,
“bucket_id_1”: “abc”,
“articleattribute1”: “abc”,
“articleattribute2”: “abc”,
“articleattribute3”: “2233”,
“article_id”: “12345”,
“location”: “blore”
},
{
“item_attr1”: “abc”,
“item_attr2”: “123”,
“item_attr3”: “123”,
“item_id”: “1234885”,
“bucket_attr1”: 1919,
“bucket_attr2”: “abc”,
“bucket_attr3”: 1922,
“bucket_attr4”: “abc”,
“bucket_id_1”: “cab”,
“articleattribute1”: “abc”,
“articleattribute2”: “abc”,
“articleattribute3”: “2233”,
“article_id”: “12345”,
“location”: “hyd”
},
{
“item_attr1”: “abc”,
“item_attr2”: “123”,
“item_attr3”: “123”,
“item_id”: “1234885”,
“bucket_attr1”: 1919,
“bucket_attr2”: “abc”,
“bucket_attr3”: 1922,
“bucket_attr4”: “abc”,
“bucket_id_1”: “abc”,
“articleattribute1”: “abc”,
“articleattribute2”: “abc”,
“articleattribute3”: “2233”,
“article_id”: “12345”,
“location”: “chennai”
},
{
“item_attr1”: “abc”,
“item_attr2”: “123”,
“item_attr3”: “123”,
“item_id”: “805045”,
“bucket_attr1”: 1919,
“bucket_attr2”: “abc”,
“bucket_attr3”: 1922,
“bucket_attr4”: “abc”,
“bucket_id_2”: “abc”,
“bucket_id_1”: “mhf”,
“articleattribute1”: “abc”,
“articleattribute2”: “abc”,
“articleattribute3”: “2233”,
“article_id”: “8050345”
},
{
“item_attr1”: “abc”,
“item_attr2”: “123”,
“item_attr3”: “123”,
“item_id”: “86600”,
“bucket_attr1”: 1919,
“bucket_attr2”: “abc”,
“bucket_attr3”: 1922,
“bucket_attr4”: “def”,
“bucket_id_1”: “abc”,
“bucket_id_2”: “ghi”,
“bucket_id_3”: “srk”,
“articleattribute1”: “abc”,
“articleattribute2”: “abc”,
“articleattribute3”: “2233”,
“article_id”: “86000aaaa”
}
Output JSON:
{
“OrderPreps” : [ {
“bucket_attr1” : 1919,
“bucket_attr2” : “abc”,
“bucket_attr3” : 1922,
“bucket_attr4” : “abc”,
“bucket_id” : “abc”,
“location” : “blore”,
“articles” : [ {
“items” : [ {
“item_id” : “1234885”,
“item_attr1” : “abc”,
“item_attr2” : “123”,
“item_attr3” : “123”
}, {
“item_id” : “1234885”,
“item_attr1” : “abc”,
“item_attr2” : “123”,
“item_attr3” : “123”
} ],
“article_id” : “12345”,
“articleattribute1” : “abc”,
“articleattribute2” : “abc”,
“articleattribute3” : “2233”
}, {
“items” : [ {
“item_id” : “805045”,
“item_attr1” : “abc”,
“item_attr2” : “123”,
“item_attr3” : “123”
} ],
“article_id” : “8050345”,
“articleattribute1” : “abc”,
“articleattribute2” : “abc”,
“articleattribute3” : “2233”
}, {
“items” : [ {
“item_id” : “86600”,
“item_attr1” : “abc”,
“item_attr2” : “123”,
“item_attr3” : “123”
} ],
“article_id” : “86000aaaa”,
“articleattribute1” : “abc”,
“articleattribute2” : “abc”,
“articleattribute3” : “2233”
} ]
}, {
“bucket_attr1” : 1919,
“bucket_attr2” : “abc”,
“bucket_attr3” : 1922,
“bucket_attr4” : “abc”,
“bucket_id” : “cab”,
“location” : “hyd”,
“articles” : [ {
“items” : [ {
“item_id” : “1234885”,
“item_attr1” : “abc”,
“item_attr2” : “123”,
“item_attr3” : “123”
} ],
“article_id” : “12345”,
“articleattribute1” : “abc”,
“articleattribute2” : “abc”,
“articleattribute3” : “2233”
} ]
}, {
“bucket_attr1” : 1919,
“bucket_attr2” : “abc”,
“bucket_attr3” : 1922,
“bucket_attr4” : “abc”,
“bucket_id” : “mhf”,
“articles” : [ {
“items” : [ {
“item_id” : “805045”,
“item_attr1” : “abc”,
“item_attr2” : “123”,
“item_attr3” : “123”
} ],
“article_id” : “8050345”,
“articleattribute1” : “abc”,
“articleattribute2” : “abc”,
“articleattribute3” : “2233”
} ]
}, {
“bucket_attr1” : 1919,
“bucket_attr2” : “abc”,
“bucket_attr3” : 1922,
“bucket_attr4” : “def”,
“bucket_id” : “ghi”,
“articles” : [ {
“items” : [ {
“item_id” : “86600”,
“item_attr1” : “abc”,
“item_attr2” : “123”,
“item_attr3” : “123”
} ],
“article_id” : “86000aaaa”,
“articleattribute1” : “abc”,
“articleattribute2” : “abc”,
“articleattribute3” : “2233”
} ]
}, {
“bucket_attr1” : 1919,
“bucket_attr2” : “abc”,
“bucket_attr3” : 1922,
“bucket_attr4” : “def”,
“bucket_id” : “srk”,
“articles” : [ {
“items” : [ {
“item_id” : “86600”,
“item_attr1” : “abc”,
“item_attr2” : “123”,
“item_attr3” : “123”
} ],
“article_id” : “86000aaaa”,
“articleattribute1” : “abc”,
“articleattribute2” : “abc”,
“articleattribute3” : “2233”
} ]
} ]
}
Jolt Spec:
[
{
“operation”: “shift”,
“spec”: {
//Iterate each dict in array
““: {
//check if bucket_id_3 is present
“bucket_id_“: “[&1].buckets”,
“location”: “[&1].location”,
“bucket_attr1”: “[&1].bucket_attr1”,
“bucket_attr2”: “[&1].bucket_attr2”,
“bucket_attr3”: “[&1].bucket_attr3”,
“bucket_attr4”: “[&1].bucket_attr4”,
“item_id”: “[&1].item_id”,
“item_attr1”: “[&1].item_attr1”,
“item_attr2”: “[&1].item_attr2”,
“item_attr3”: “[&1].item_attr3”,
“article_id”: “[&1].article_id”,
“articleattribute1”: “[&1].articleattribute1”,
“articleattribute2”: “[&1].articleattribute2”,
“articleattribute3”: “[&1].articleattribute3”
}
}
},
{
“operation”: “cardinality”,
“spec”: {
““: {
“buckets”: “MANY”
}
}
},
{ // 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”
}
}
}
},
{ // group by bucket_id values
“operation”: “shift”,
“spec”: {
““: {
““: “@(1,bucket_id).&”,
“article_id|articleattribute2|articleattribute3|articleattribute1”: {
“@”: “@(2,bucket_id).articles.@(2,article_id).&”
},
“item_id|item_attr1|item_attr2|item_attr3”: {
“@”: “@(2,bucket_id).articles.@(2,article_id).items[&2].&”
}
}
}
},
{ // nest all JSON value within OrderPreps array
“operation”: “shift”,
“spec”: {
““: “OrderPreps[]”
}
},
{
“operation”: “cardinality”,
“spec”: {
““: {
““: {
““: “ONE”,
“articles”: {
““: {
““: “ONE”,
“items”: “MANY”
}
}
}
}
}
},
{ // get rid of redundant nulls
“operation”: “modify-overwrite-beta”,
“spec”: {
““: “=recursivelySquashNulls”
}
},
{
“operation”: “shift”,
“spec”: {
// Retain other attributes at the same level as “articles”
“OrderPreps”: {
// Iterate over each entry in “OrderPreps”
““: {
// Retain specific attributes
“bucket_attr1”: “OrderPreps[&1].bucket_attr1”,
“bucket_attr2”: “OrderPreps[&1].bucket_attr2”,
“bucket_attr3”: “OrderPreps[&1].bucket_attr3”,
“bucket_attr4”: “OrderPreps[&1].bucket_attr4”,
“bucket_id”: “OrderPreps[&1].bucket_id”,
“location”: “OrderPreps[&1].location”,
// Shift the “articles” object
“articles”: {
// Iterate over each key in “articles”
“*”: {
// Append the value of each key to the “articles” array
“@”: “OrderPreps[&3].articles[]”
}
}
}
}
}
}
]
Spec is working fine to nest the data based on bucket_id, but now it has to be nested based on both bucket_id and location keys
Jeevan Kumar K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.