英文:
JOLT- Get duplicated field value from multiple objects in array and make it one object
问题
[
{
"code":"ABC"
},
{
"code":"DEF"
}
]
英文:
I am attempting to transform JSON to JSON in the below example. I am getting some events from MQ with JSON payload with multiple objects with one common code. I am interested to get unique code as one JSON object. Below is the input data and expected output.
Input JSON
[
{
"name":"data1",
"event":"MAP",
"payload":{
"event":"ADDED",
"code":"ABC"
}
},
{
"name":"data2",
"event":"MAP",
"payload":{
"event":"ADDED",
"code":"ABC"
}
},
{
"name":"data3",
"event":"MAP",
"payload":{
"event":"ADDED",
"code":"ABC"
}
},
{
"name":"data4",
"event":"MAP",
"payload":{
"event":"ADDED",
"code":"DEF"
}
},
{
"name":"data5",
"event":"MAP",
"payload":{
"event":"ADDED",
"code":"DEF"
}
},
{
"name":"data6",
"event":"MAP",
"payload":{
"event":"ADDED",
"code":"DEF"
}
}
]
Expected Output
[
{
"code":"ABC"
},
{
"code":"DEF"
}
]
JOLT spec tried
[
{
"operation": "shift",
"spec": {
"*": {
"@payload.code": "[&1].code"
}
}
}
]
Output getting
[
{
"code":"ABC"
},
{
"code":"ABC"
},
{
"code":"ABC"
},
{
"code":"DEF"
},
{
"code":"DEF"
},
{
"code":"DEF"
}
]
答案1
得分: 1
[
{
"operation": "shift",
"spec": {
"*": {
"# ": "@(1,payload.code)"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"$": "[#2].code"
}
}
}
]
英文:
An option to group by a value is to keep that identifier(@(1,payload.code)
) , which produces the value, on the right hand side such as
[
{ // get unique keys "ABC" and "DEF"
"operation": "shift",
"spec": {
"*": {
"# ": "@(1,payload.code)"
}
}
},
{ // create array of objects which contain attributes with values taken from the previously derived keys
"operation": "shift",
"spec": {
"*": {
"$": "[#2].code"
}
}
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论