2

How might one turn this:

{
  "write": {
    "out": 0,
    "available": 128,
    "totalTickets": 128
  },
  "read": {
    "out": 7,
    "available": 249,
    "totalTickets": 256
  }
}

Into

0 128 128 7 249 256

Using jq?

0
2

You can also recursively descend your JSON tree like that:

>jq 'recurse|numbers' data.json
0
128
128
7
249
256

(this assumes that all your values are numbers, use scalars if you also want booleans and strings)

Or use a shorthand notation for recurse, which is ..

..|numbers

(Thanks @cuonglm !)

Note, that unlike map|flatten approach this will also work for an arbitrary nested tree of objects.

E.g.

{
  "write": {
    "out": 0,
    "available": 128,
    "totalTickets": 128
  },
  "read": {
    "out": 7,
    "available": 249,
    "totalTickets": 256,
    "details" : {
        "good": 10,
        "bad" : 20
    }
  }
}

will produce:

0
128
128
7
249
256
10
20
3
  • or shorter jq '..|numbers'
    – cuonglm
    Feb 1 '17 at 17:38
  • Dang that's neat...
    – Kusalananda
    Feb 1 '17 at 17:45
  • With the slight modifier of command | jq -r 'recurse|numbers' | paste -s this did exactly what I needed. Thanks. Feb 2 '17 at 12:57
1
$ jq 'map(map(values))|flatten|.[]' data.json
0
128
128
7
249
256

The first part generates

[
  [
    0,
    128,
    128
  ],
  [
    7,
    249,
    256
  ]
]

This is flattened into:

[
  0,
  128,
  128,
  7,
  249,
  256
]

And the .[] extracts those values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.