gpt4 book ai didi

json - jq 按键值排序

转载 作者:行者123 更新时间:2023-12-04 13:12:22 27 4
gpt4 key购买 nike

鉴于以下 JSON(为了示例而过度简化),我需要按键的值对键进行排序。在这种情况下,顺序应该是 id > name > type。

{
"link": [{
"attributes": [{
"value": "ConfigurationElement",
"name": "type"
}, {
"value": "NAME1",
"name": "name"
}, {
"value": "0026a8b4-ced6-410e-9213-e3fcb28b3aab",
"name": "id"
}
],
"href": "href1",
"rel": "down"
}, {
"attributes": [{
"value": "0026a8b4-ced6-410e-9213-k23g15h2u1l5",
"name": "id"
}, {
"value": "ConfigurationElement",
"name": "type"
}, {
"value": "NAME2",
"name": "name"
}
],
"href": "href2",
"rel": "down"
}
],
"total": 2
}
预期结果:
{
"link": [{
"attributes": [{
"value": "0026a8b4-ced6-410e-9213-e3fcb28b3aab",
"name": "id"
}, {
"value": "NAME1",
"name": "name"
}, {
"value": "ConfigurationElement",
"name": "type"
}
],
"href": "href1",
"rel": "down"
}, {
"attributes": [{
"value": "0026a8b4-ced6-410e-9213-k23g15h2u1l5",
"name": "id"
}, {
"value": "NAME2",
"name": "name"
}, {
"value": "ConfigurationElement",
"name": "type"
}
],
"href": "href2",
"rel": "down"
}
],
"total": 2
}
如果有人能帮助我,我将不胜感激。我用-S 和-s 用sort_by() 尝试过jq,但是这个例子太复杂了,我无法根据我目前的jq 经验弄清楚。非常感谢!

最佳答案

你可以做:

jq '.link[].attributes|=sort_by(.name)'
|=.link[].attributes匹配的所有路径,即每个“属性”数组,并应用过滤器 sort_by(.name)给他们每个人,让其他一切保持不变。

关于json - jq 按键值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63739892/

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