From jq's Types And Values Documentation:
Object Construction
The value can be any expression (although you may need to wrap it in parentheses if, for example, it contains colons), which gets applied to the
{}expression's input (remember, all filters have an input and an output).{foo: .bar}[ ... ]
Because that is so common, there's a shortcut syntax for it:
{user, title}.
So given the following example json
{
"foo": {
"bar": {
"value": 42,
"valid": true
},
"valid": false
},
"valid": false
}
If I only want the complete foo object (removing the outer valid key), I can get the desired output by using
{ foo }
Unfortunately, the documentation doesn't mention any way to get the same behaviour, but for deeper keys, using the example above, getting an output object with just the .foo.bar object would look something like:
{ "foo": { "bar": .foo.bar } }
Which will generate the following (desired) json output:
{
"foo": {
"bar": {
"value": 42,
"valid": true
}
}
}
Note both the outer valid and the .foo.valid are missing.
Is there any short-cut alternative for the above selector to get the same result?
Ideally I'd use something like:
{ foo.bar }