将JSON转换为压缩格式
我正在尝试转换以下json将JSON转换为压缩格式,json,jq,Json,Jq,我正在尝试转换以下json { "application" : [ { "name" : "app1", "policies" : [ { "name" : "pol_1", "orderNumber" : "10"
{ "application" : [
{ "name" : "app1",
"policies" : [
{ "name" : "pol_1",
"orderNumber" : "10"
},
{ "name" : "pol_2",
"orderNumber" : "20"
}
]
},
{ "name" : "app2",
"policies" : [
{ "name" : "pol_A",
"orderNumber" : "10"
},
{ "name" : "pol_B",
"orderNumber" : "20"
}
]
}
]
}
以下
{ "pol_1":"10", "pol_2":"20" }
使用
我能够得到
{
"pol_1":"10"
}
{
"pol_2":"20"
}
你知道我该怎么合并他们吗。我是错过了什么还是我做错事了?你就快到了。使用
map
创建单个数组而不是两个独立的对象,然后使用add
合并其内容
jq '.application[]
| select(.name == "app1")
| .policies
| map({ (.name) : .orderNumber } )
| add' file.json
jq '.application[]
| select(.name == "app1")
| .policies
| map({ (.name) : .orderNumber } )
| add' file.json