命令:
cat test.json | jq -r '.[] | select(.["$link"] | contains("randomtext1")).id'
我希望两个 id(a 和 b)都显示为上述命令的结果,因为它们都包含 "randomtext1"
text under
"input"|"obj1"|"$link" and "input"|"obj3"|"$link".
jq: error (at <stdin>:11): null (null) and string ("randomtext1") cannot have their containment checked
parse error: Expected value before ',' at line 11, column 2
我看到 "$link"
对象在 "input"
和 "obj#"
对象中。我如何指定它们或我是否需要指定它们?错误消息似乎指向其他内容。此外,只有 "input"
和 "$link"
在记录中是不变的,"obj#"
的名称可以更改,这使得它们是随机的。
test.json 文件:
[{
"input": {
"obj1": {
"$link": "randomtext1"
},
"obj2": {
"$link": "randomtext2"
}
},
"id": "a"
},
{
"input": {
"obj3": {
"$link": "randomtext1"
},
"obj4": {
"$link": "randomtext3"
}
},
"id": "b"
}]
最佳答案
过滤器:
.[]
| select(.input[] | .["$link"] | contains("randomtext1"))
| .id
产生:
"a"
"b"
但是请注意,contains
具有非常复杂的语义,因此使用 index
可能会更好。
关于select - jq 选择并包含错误 : null (null) and string ("randomtext1") cannot have their containment checked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55661161/