Given a list of objects, with many keys I don't want:
[{
"name": "Alice",
"group": "Admins",
"created": "2014"
}, {
"name": "Bob",
"group": "Users",
"created": "2014"
}]
How do I filter these objects to only include keys I want?
[{
"name": "Alice"
}, {
"name": "Bob"
}]
I've tried jq '.[].name'
but that extracts the values, rather than preserving the objects.
you can use
map
withdel
if you know the keys you don't want:Another solution without the
map
function:The accepted answer (with
map
) and the equivalent answer by @mauricio-tranjano will, in effect, add the specified key to objects that don't already have it. If that's not the behavior you want, then consider usinghas(_)
, e.g.:Input:
Output:
You can use the
map()
function to filter any key:Update
Suggested by
@WilfredHughes
: The above filter can be abbreviated as follows: