json - Select or exclude multiples object with an array of IDs -


i have following json :

[   {     "id": "1",     "foo": "bar-a",     "hello": "world-a"   },   {     "id": "2",     "foo": "bar-b",     "hello": "world-b"   },   {     "id": "10",     "foo": "bar-c",     "hello": "world-c"   },   {     "id": "42",     "foo": "bar-d",     "hello": "world-d"   } ] 

and have following array store in variable: ["1", "2", "56", "1337"] (note ids string, , may contain regular character).

so, to so, found way filter original data. jq 'jq '[.[] | select(.id == ("1", "2", "56", "1337"))]' ./data.json (note array surrounded parentheses , not brackets) produces :

[   {     "id": "1",     "foo": "bar-a",     "hello": "world-a"   },   {     "id": "2",     "foo": "bar-b",     "hello": "world-b"   } ] 

but liked opposite (basically excluding ids instead of selecting them). using select(.id != ("1", "2", "56", "1337")) doesn't work , using jq '[. - [.[] | select(.id == ("1", "2", "56", "1337"))]]' ./data.json seems ugly , doesn't work actual data (an output of aws ec2 describe-instances).

so have idea that? thank you!

to include them, need verify id of values in keep set.

$ jq --argjson include '["1", "2", "56", "1337"]' 'map(select(.id == $include[]))' ... 

to exclude them, need verify values not in excluded set. might easier take original set , remove items in excluded set.

$ jq --argjson exclude '["1", "2", "56", "1337"]' '. - map(select(.id == $exclude[]))' ... 

Comments