k0pernikus k0pernikus - 5 days ago 5
JSON Question

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

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?

Answer

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"
}
Comments