Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQ select filter with multiple arguments [duplicate]

Tags:

json

jq

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.

like image 438
ccpizza Avatar asked Oct 02 '17 16:10

ccpizza


1 Answers

Try this:

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

Demo

like image 99
bhansa Avatar answered Oct 17 '22 04:10

bhansa