[英]Reduce array of dicts based on key which is a dict
我正在尋找一個 jq 表達式,它可以通過將它們分組在“標簽”上來減少 json。我希望輸出具有共享相同標簽的目標列表。
我嘗試使用 group_by 和 reduce 的一些組合,但無法得到我想要的。
輸入:
[{
"targets": [
"host1"
],
"labels": {
"platform": "VMware",
"os": "Windows",
"datacenter": "dc1",
"environment": "Production"
}
},{
"targets": [
"host2"
],
"labels": {
"platform": "VMware",
"os": "Windows",
"datacenter": "dc1",
"environment": "Production"
}
},
{
"targets": [
"host3"
],
"labels": {
"platform": "VMware",
"os": "Windows",
"datacenter": "dc2",
"environment": "Production"
}
}
]
輸出:
[{
"targets": [
"host1",
"host2"
],
"labels": {
"platform": "VMware",
"os": "Windows",
"datacenter": "dc1",
"environment": "Production"
}
},
{
"targets": [
"host3",
],
"labels": {
"platform": "VMware",
"os": "Windows",
"datacenter": "dc2",
"environment": "Production"
}
}
]
PS 示例輸入沒有“標簽”中的所有鍵,該列表可能會有所不同。
如果您的要求更清晰,特別是如果您的示例滿足mcve准則,那將會很有幫助。 但是,以下查詢確實符合對您的規范的一種解釋:
group_by(.labels)
| map( { labels: (.[0].labels),
targets: (map(.targets)|add)} )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.