I'm trying to group the json elements based on id, name, type like below:
input:
[
{
"itemid": "1",
"itemName": "coco",
"itemType": "brg",
"subitemId": "444",
"subitemName": "INDICATIVE"
},
{
"itemid": "1",
"itemName": "coco",
"itemType": "brg",
"subitemId": "333",
"subitemName": "BRGS"
},
{
"itemid": "2",
"itemName": "limk",
"itemType": "cmds",
"subitemId": "456",
"subitemName": "NMPS"
}
]
expected output:
[
{
"itemid": "1",
"itemName": "coco",
"itemType": "brg",
"subitems": [
{
"subitemId": "444",
"subitemName": "INDICATIVE"
},
{
"subitemId": "333",
"subitemName": "BRGS"
}
]
},
{
"itemid": "2",
"itemName": "limk",
"itemType": "cmds",
"subitems": [
{
"subitemId": "456",
"subitemName": "NMPS"
}
]
}
]
i have tried the below spec but it is not giving expected output:
[
{
"operation": "shift",
"spec": {
"*": {
"subitemId": "subitems[&1].subitemId",
"subitemName": "subitems[&1].subitemName",
"*": "&"
}
}
}
]
Could you please help me.
This spec should work for you
[
{
"operation": "modify-default-beta",
"spec": {
"*": {
"comp_id": "=concat(@(1,itemid),'_', @(1,itemName), '_', @(1,itemType))"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "@(1,comp_id).&[]"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[#2].&",
"comp_id": null
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"itemid": "=firstElement(@(1,itemid))",
"itemName": "=firstElement(@(1,itemName))",
"itemType": "=firstElement(@(1,itemType))"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[&1].&",
"subitemId": {
"*": "[&2].subitems[&].subitemId"
},
"subitemName": {
"*": "[&2].subitems[&].subitemName"
}
}
}
}
]
Tested with https://jolt-demo.appspot.com
Try to examine it operation by operation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.