0

there is json data like this

➜  ~ cat foo.json | jq
{
  "data": {
    "foo": [
      {
        "table": "aaa"
      },
      {
        "table": "bbb"
      }
    ],
    "bar": [
      {
        "table": "ccc"
      },
      {
        "table": "ddd"
      }
    ]
  }
}

Could get table of foo or bar separately,

➜  ~ cat foo.json | jq '.data.foo[].table'
"aaa"
"bbb"
➜  ~ cat foo.json | jq '.data.bar[].table'
"ccc"
"ddd"

how could get all table values in one command?

peak
  • 105,803
  • 17
  • 152
  • 177
zhuguowei
  • 8,401
  • 16
  • 70
  • 106

2 Answers2

1

Use the comma operator.

$ jq '.data["foo", "bar"][].table' foo.json

or

$ jq '.data | .foo, .bar | .[].table' foo.json

Or assuming you want to grab any property of the data object.

$ jq '.data[][].table' foo.json
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
0

If you don't care where the "table" values occur, and if you want to exclude null and false values:

.. | .table? // empty

If you want to include null and false values:

.. | objects | has("table") | .table
peak
  • 105,803
  • 17
  • 152
  • 177