5

Given the json filter.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:

"Horst"
"Bob"
[
  "123",
  "789"
]

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

k0pernikus
  • 60,309
  • 67
  • 216
  • 347

2 Answers2

6

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"
}
Community
  • 1
  • 1
0
$ jq -r 'keys[] as $key | "\"\($key)\": \"\(.[$key].name)\""

produces:

"123": "Horst"
"789": "Bob"
peak
  • 105,803
  • 17
  • 152
  • 177