【发布时间】:2017-12-14 23:44:50
【问题描述】:
我有一个特定的 json 内容,我需要获取所有在其值中包含字符 / 的键。
JSON
{ "dig": "sha256:d2aae00e4bc6424d8a6ae7639d41cfff8c5aa56fc6f573e64552a62f35b6293e",
"name": "example",
"binding": {
"wf.example.input1": "/path/to/file1",
"wf.example.input2": "hello",
"wf.example.input3":
["/path/to/file3",
"/path/to/file4"],
"wf.example.input4": 44
}
}
我知道我可以使用查询 jq 'paths(type == "string" and contains("/"))' 获取包含文件路径或文件路径数组的所有键。这会给我这样的输出:
[ "binding", "wf.example.input1" ]
[ "binding", "wf.example.input3", 0]
[ "binding", "wf.example.input3", 1 ]
现在我拥有包含一些文件路径作为其值的所有元素,有没有办法同时获取相同的键和值,然后将它们存储为另一个 JSON?例如,在针对这个问题提到的 JSON 中,我需要将输出作为另一个包含所有匹配路径的 JSON。我的输出 JSON 应该如下所示。
{ "binding":
{ "wf.example.input1": "/path/to/file1",
"wf.example.input3": [ "/path/to/file3", "/path/to/file4" ]
}
}
【问题讨论】: