Flat JSON to Nested JSON on 2 common fields

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

New contributor

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật