151

How do I get jq to take json like this:

{
  "host1": { "ip": "10.1.2.3" },
  "host2": { "ip": "10.1.2.2" },
  "host3": { "ip": "10.1.18.1" }
}

and generate this output:

host1, 10.1.2.3
host2, 10.1.2.2
host3, 10.1.18.1

I'm not interested in the formatting, I just can't figure out how to access the key name and value.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Jeff Tang
  • 1,723
  • 3
  • 11
  • 7

2 Answers2

247

To get the top-level keys as a stream, you can use the built-in function keys[]. So one solution to your particular problem would be:

jq -r 'keys[] as $k | "\($k), \(.[$k] | .ip)"' 

keys produces the key names in sorted order; if you want them in the original order, use keys_unsorted.

Another alternative, which produces keys in the original order, is:

jq -r 'to_entries[] | "\(.key), \(.value | .ip)"'

CSV and TSV output

The @csv and @tsv filters might also be worth considering here, e.g.

jq -r 'to_entries[] | [.key, .value.ip] | @tsv'

produces:

host1   10.1.2.3
host2   10.1.2.2
host3   10.1.18.1

Embedded objects

If the keys of interest are embedded as in the following example, the jq filter would have to be modified along the lines shown.

Input:

{
  "myhosts": {
    "host1": { "ip": "10.1.2.3" },
    "host2": { "ip": "10.1.2.2" },
    "host3": { "ip": "10.1.18.1" }
  }
}

Modification:

jq -r '.myhosts | keys[] as $k | "\($k), \(.[$k] | .ip)"'
peak
  • 105,803
  • 17
  • 152
  • 177
  • Suppose the ".ip" key had whitespace in it, for example, "ip address", how would escape the quotes surrounding the key, given that you would be on the third level of a quoted string, if that makes sense? – 170730350 Jul 27 '20 at 20:02
  • 1
    Yes, it makes sense, and it's a good question, but forgive me for saying, you could easily have answered it yourself, since the answer is simply to ignore the potential complication. That is, the normal approach would have been to quote the string-with-spaces, et voilà. – peak Jul 27 '20 at 21:11
  • I had tried that without success. Tried escaping the quotes but it didn't work, hence the question. – 170730350 Jul 27 '20 at 21:25
  • 1
    @Saichovsky - I've checked it works in jq 1.4, 1.5, and 1.6. If you're using jq 1.3, you'd have to write `.["id address"]`, as you would have to do in the normal case anyway. – peak Jul 27 '20 at 23:29
98

Came across very elegant solution

jq 'with_entries(.value |= .ip)'

Which ouputs

{
  "host1": "10.1.2.3",
  "host2": "10.1.2.2",
  "host3": "10.1.18.1"
}

Here is the jqplay snippet to play with: https://jqplay.org/s/Jb_fnBveMQ

The function with_entries converts each object in the list of objects to Key/Value-pair, thus we can access .key or .value respectively, we're updating (overwriting) every KV-item .value with the field .ip by using update |= operator

Viacheslav
  • 1,325
  • 9
  • 12
  • 2
    This solution is the exact answer. The selected solution has extras that are helpful but not as exact or elegant. You may want to add a concise explanation and the full command. – Mike D Mar 17 '18 at 01:09
  • 1
    Superb! Many times I used "to_entries[]" simply because I didn't know this one. – Lungang Fang Feb 06 '20 at 01:42