Output the results of select operation in an array – jq

Yes; wrap the filter in an array 🙂

$ jq '[.[] | select(.id == "second")]' tmp.json
[
  {
    "id": "second",
    "val": 2
  },
  {
    "id": "second",
    "val": 3
  }
]

Or, use map/1, which is predefined as [.[] | ...].

$ jq 'map(select(.id == "second"))' tmp.json
[same result]

To wrap the results in a bash array, use the -c option to output each result on a single line, and read the result with readarray.

$ readarray -t arr < <(jq -c '.[] | select(.id == "second")' tmp.json)
$ for r in "${arr[@]}"; do echo "Result: $r"; done
Result: {"id":"second","val":2}
Result: {"id":"second","val":3}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)