我这里有两个 json 文件,它们都有相同的内容,只是顺序不同,并且要通过 diff 检查它们是否相等。
我已经使用 jq -S 对键进行了排序,但现在我必须确保字符串在数组中同等排序。
不幸的是,我现在失败了,我不太清楚如何达到正确的级别以及如何对内容进行排序。
这是 json 的示例结构,数组“allowed-test-mapper-data”应按降序排序
{
"accessCodeLife": 60,
"accessCodeLifespan": 1800,
"accessCodeType": 300,
"components": {
"test.data.app": [
{
"config": {
"allow-default-test-scopes": [
"true"
]
},
"name": "Allowed Test Client",
"id": "allowed-testdata",
"subComponents": {},
"subType": "testdata"
},
{
"config": {
"allowed-test-mapper-data": [
"alfred",
"usa",
"canada",
"somedata",
"alcohol",
"brother"
]
}
}
]
}
}
有人可以帮我吗? 那就太好了:)
最佳答案
使用更新赋值|=
运算符来更改结构的一部分:
jq '.components."test.data.app"[].config."allowed-test-mapper-data"
|= if . then sort else empty end' file.json
关于json - 使用 jq 对数组中的字符串进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69110148/