Filter Out Results Based On List Of Values
November 27, 2023 ยท View on GitHub
Let's say we have an array of objects in a JSON file. We want to extract some data about each of those objects, but first we want to filter out some of the objects that we don't need. This will be based on a list of IDs.
The JSON might look something like this:
[
{'id': '123', ...},
{'id': '456', ...},
{'id': '789', ...},
{'id': '963', ...},
...
]
With the select function, we
can filter the array down to those objects whose IDs are
not
inside the list of IDs to
exclude.
jq '.[] | select([.id] | inside["456", "963"] | not)' data.json
Inside that select, we grab the id as a single value array, check if that
value is inside our exclude array, and then invert that result. If there is a
match, that object will be filtered out.
We can then chain additional filtering and extraction on to the end of the query to produce the result we want.