gpt4 book ai didi

json - 如何在jq中分组?

转载 作者:行者123 更新时间:2023-12-05 06:21:10 26 4
gpt4 key购买 nike

这是json文件

[
{"name": "bucket1","clusterName":"cluster1"},
{"name": "bucket2","clusterName":"cluster1"},
{"name": "bucket3","clusterName":"cluster2"},
{"name": "bucket4","clusterName":"cluster2"}
]

我想把它转换成

[
{"clusterName": "cluster1", buckets:[{"name": "bucket1"}, {"name": "bucket2"}]},
{"clusterName": "cluster2", buckets:[{"name": "bucket1"}, {"name": "bucket2"}]},
]

我如何在 jq 中做到这一点?

最佳答案

cat doc.json | jq '[group_by(.clusterName)[] | { clusterName: .[0].clusterName, "buckets": [.[] | { name: .name } ]}]'

应该做你想做的。现在还有一个数组。

关于json - 如何在jq中分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60011507/

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