命令:
cat test.json | jq -r '.[] | select(.["$link"] | contains("randomtext1")).id'
我希望上面的命令会同时显示两个I (a和b),因为它们都包含下面的"randomtext1"
文本
"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"
}]
发布于 2019-04-13 09:40:26
过滤器:
.[]
| select(.input[] | .["$link"] | contains("randomtext1"))
| .id
产生:
"a"
"b"
但是请注意,contains
具有非常复杂的语义,因此使用index
可能会更好。
https://stackoverflow.com/questions/55661161
复制相似问题