Get values for a given key and its parent with jq

$ jq -r 'keys[] as $k | "\($k) \(.[$k].conns)"' file.json
1 1
2 2

Seems like jq translates 1.0 to 1 and 2.0 to 2. Altering the input for clarity:

$ cat file.json
{
  "1a": {
    "available_memory": 1086419656.0,
    "available_memory_no_overbooking": 1086419656.0,
    "conns": 1.1
  },
  "2b": {
    "available_memory": 108641236.0,
    "available_memory_no_overbooking": 10861216.0,
    "conns": 2.2
  }
}
$ jq -r 'keys[] as $k | "\($k) \(.[$k].conns)"' file.json
1a 1.1
2b 2.2

Refs:

  • https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...
  • https://stedolan.github.io/jq/manual/#Stringinterpolation-\(foo)

Tags:

Json

Jq