3

命令:

cat test.json | jq -r '.[] | select(.["$link"] | contains("randomtext1")).id'

我期望两个 id(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"and"obj#"对象内。我如何指定它们还是我需要?错误消息似乎指向其他内容。也只有"input""$link"在记录中是不变的,名称"obj#"可以改变,这使得它们是随机的。

test.json 文件:

[{
      "input": {
            "obj1": {
                "$link": "randomtext1"
            }, 
            "obj2": {
                "$link": "randomtext2"
            }
      },
      "id": "a"
},
{
      "input": {
            "obj3": {
                "$link": "randomtext1"
            }, 
            "obj4": {
                "$link": "randomtext3"
            }
      },
      "id": "b"
}]
4

1 回答 1

0

过滤器:

.[]
| select(.input[] | .["$link"] | contains("randomtext1")) 
| .id

产生:

"a"
"b"

但是请注意,它contains具有非常复杂的语义,因此使用它可能会更好index

于 2019-04-13T01:40:26.270 回答