Wilfred Hughes Wilfred Hughes - 1 year ago 199
JSON Question

How do I remove all keys except one with jq?

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.

Answer Source

you can use map() function to filter any key, see below command

jq 'map({name: .name})'

Update Suggested by @WilfredHughes Shorten is possible without key definition jq automatic add key

jq 'map({name})'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download