How to filter array of objects by element property values using jq?
From the docs:
jq '.[] | select(.id == "second")'
Input
[{"id": "first", "val": 1}, {"id": "second", "val": 2}]
Output
{"id": "second", "val": 2}
I think you can do something like this:
jq '.theList[] | select(.id == 2 or .id == 4)' array.json
You could use select
within map
.
.theList | map(select(.id == (2, 4)))
Or more compact:
[ .theList[] | select(.id == (2, 4)) ]
Though written that way is a little inefficient since the expression is duplicated for every value being compared. It'll be more efficient and possibly more readable written this way:
[ .theList[] | select(any(2, 4; . == .id)) ]