k0pernikus k0pernikus - 1 year ago 117
JSON Question

How to select key and sub-object property via jq?

Given the json


"123": {
"name": "Horst"
"789": {
"name": "Bob"

I want to filter for each key and the name, wanting an output like:

"123": "Horst"
"789": "Bob"

I tried:

jq .[].name,keys < filter.json

Yet it gives me faulty output of:


yet I don't know how to "merge" these two outputs to one. Where am I going wrong?

Answer Source

This question is similar I took the best answer from there a tweaked it a little bit to get the following.

$ jq 'to_entries[]| {(.key): .value.name}' < /tmp/filter.json
  "123": "Horst"
  "789": "Bob"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download