[英]Json parsing on cli using jq
Let's say I have the below json object: 假设我有以下json对象:
{
"d": {
"e": {
"bar": 2
}
},
"a": {
"b": {
"c": {
"foo": 1
}
}
}
}
I want to get the value foo without typing '.abcfoo'
我想在不输入'.abcfoo'
情况下获取值foo
I realize I can do... echo '{ "a":{"b":{"c":{ "foo":1}}},"d":{"e":{"bar":2}}}' | jq '.[][][].foo'
我知道我可以做... echo '{ "a":{"b":{"c":{ "foo":1}}},"d":{"e":{"bar":2}}}' | jq '.[][][].foo'
echo '{ "a":{"b":{"c":{ "foo":1}}},"d":{"e":{"bar":2}}}' | jq '.[][][].foo'
but is there a recursive wild in jq? echo '{ "a":{"b":{"c":{ "foo":1}}},"d":{"e":{"bar":2}}}' | jq '.[][][].foo'
但是jq中是否存在递归野生型? like **? 喜欢 **? I know for sure jq doesn't support *, is there a way to have jq support jsonpath? 我知道jq不支持*,有没有办法让jq支持jsonpath? Or maybe even just another cli tool that does support json path? 也许只是另一个不支持json路径的cli工具?
In jq 1.4 you could do this: 在jq 1.4中,您可以执行以下操作:
$ jq '..|.foo?' file.json
If you're stuck with 1.3 you could use 如果您坚持使用1.3,则可以使用
$ jq 'recurse(if type == "array" or type == "object" then .[] else empty end) | if type == "object" then . else empty end | .foo' file.json
which is a bit of a mouthful... That's why 1.4 has ..
, which recurses down through all iterables in .
这有点令人费解...这就是为什么1.4具有..
的原因,它遍历了.
, and the ?
和?
operator, which doesn't bother indexing that which can't be. 运算符,它不会打扰不能被索引的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.