gpt4 book ai didi

json - 使用 jq 删除 JSON 值中的重复项

转载 作者:行者123 更新时间:2023-12-04 17:17:19 24 4
gpt4 key购买 nike

我有以下 JSON:

[
{
"function": "ping",
"name": "start",
"servers": [
{
"load": 581.6875,
"last_heard": 2.379324197769165,
"version": "1.0",
"hidden": false,
"pid": "19735"
},
{
"load": 444.0625,
"last_heard": 1.3227169513702393,
"version": "1.0",
"hidden": false,
"pid": "12092"
}
]
},
{
"function": "pong",
"name": "middle",
"servers": [
{
"load": 581.6875,
"last_heard": 2.379324197769165,
"version": "2.0",
"hidden": false,
"pid": "19735"
},
{
"load": 444.0625,
"last_heard": 1.3227169513702393,
"version": "3.0",
"hidden": false,
"pid": "12092"
},
{
"load": 444.0625,
"last_heard": 1.3227169513702393,
"version": "3.0",
"hidden": false,
"pid": "12093"
}
]
},
{
"function": "pang",
"name": "end",
"servers": [
{
"load": 581.6875,
"last_heard": 2.379324197769165,
"version": "2.0",
"hidden": false,
"pid": "19735"
},
{
"load": 444.0625,
"last_heard": 1.3227169513702393,
"version": "2.0",
"hidden": false,
"pid": "12092"
}
]
}
]

(这只是一个样本,有数百个条目)

我需要的是得到
[{"name": "start", "version": ["1.0"]},
{"name": "middle", "version": ["2.0", "3.0"]},
{"name": "end", "version": ["2.0"]}]

所以我需要删除无用的数据,然后获取一个名称列表,其中包含“版本”的唯一值。

我可以得到直到我拥有的地步
{
"name": "ping",
"version": [
"1.0",
"1.0"
]
}
{
"name": "pong",
"version": [
"2.0",
"3.0",
"3.0"
]
}
{
"name": "pang",
"version": [
"2.0",
"2.0"
]
}

使用
jq '.[] | {name: .function, version: [.servers[].version]}'

但我需要摆脱重复的值。
这可以使用jq吗?

最佳答案

你快到了。只需管道 version数组到 unique功能:

jq '[.[]|{name, "version": [.servers[].version]|unique}]' input

关于json - 使用 jq 删除 JSON 值中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33766735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com