select - jq 选择并包含错误 : null (null) and string ("randomtext1") cannot have their containment checked

标签 select jq containment

命令:

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/

相关文章:

c - inotify api 在报告一两次后停止工作

json - 将解析的 JSON 导出到 CSV 文件时自定义 -jq 的分隔符

ios - 将消息从自定义容器 View Controller 传递到包含的容器 View Controller 的子级?

ios - 在存储在 NSMutableDictionary 中并由 UIViewControllerContainment 呈现的 UIViewController 上调用 Dealloc

rust - 在建模包含关系时了解生命周期管理

mysql - MAX 函数与 GROUP BY 子句一起使用

Python 和使用 ListCtrl 进行多选

json - 如何在jq walk中引用父对象?

jQuery 选择文本并在段落中添加跨度

json - 增加值并保存回文件