ccpizza ccpizza - 2 years ago 192
JSON Question

JQ select filter with multiple arguments

Given then following JSON

[
{
"id": "1",
"name": "sausage",
"value": 100
},
{
"id": "2",
"name": "spam",
"value": 200
},
{
"id": "3",
"name": "eggs",
"value": 300
}
]


I can select a single record with
id=3
with:



jq '.[] | select(.id=="3") | .name,.value' data.json
### > "sausage"
### > "100"


But how to select several
id
's, i.e. the items with
id in (1,2)
?

## this is something I wish I could do
jq '.[] | select(.id in ("1", "2") | .name,.value' data.json


I tried:

jq '.[] | select(.id=="1") or select(.id=="2") | .name,.value' data.json


but this results in an error.

Answer Source

Try this:

.[] | select(.id == "3" or .id == "2") | .name,.value

Demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download