Say I have a multilevel json like the following:
{
"array": [
1,
2,
3
],
"boolean": true,
"color": "gold",
"null": null,
"number": 123,
"object": {
"a": "b",
"c": "d"
},
"string": "Hello World"
}
Can jq return the same json but with fields that I choose on multiple levels?
e.g.
{
"object": {
"a": "b",
},
"string": "Hello World"
}
Here, I have selected the string
field at the top level, and the a
field within the object
field.
Edit: In practice, there are multiple fields on each level that I would like to select, and the levels are >4.
CodePudding user response:
Yes, it is possible. I wrote a function for that once and posted here. You can use it like so:
def pick(paths):
. as $in
| reduce path(paths) as $path (null;
setpath($path; $in | getpath($path))
);
pick(.object.a, .string)