Use the select filter of jq:

jq '.zk_kafka | .[] | select(.InstanceType == "t2.medium")'

Use the --arg option to pass an argument to the query to avoid injections.

jq --arg instance "t2.medium" '.zk_kafka | .[] | select(.InstanceType == $instance)'

jq has a manual, a tutorial and a cookbook.

Answer from ceving on Stack Overflow
Discussions

filter array of objects using jq from json - Unix & Linux Stack Exchange
The second transpose converts this array of columns into an array of rows that we pass through @csv to output CSV-formatted data. The header is created by the first line in the jq expression. More on unix.stackexchange.com
🌐 unix.stackexchange.com
How do I filter the contents of a json array, but keep the parent with jq? - Unix & Linux Stack Exchange
I just want the elements with OwnerId!=abc, but I want to keep the Vpcs parent array intact. If I do echo $JSON|jq -r '.Vpcs[]|select(.OwnerId!="abc"), I get this: More on unix.stackexchange.com
🌐 unix.stackexchange.com
January 29, 2020
JQ - filter datasets in array based on index value within each dataset of array.
Inside that JSON file I am only interested in the data that exists with a certain value in one of the indexes within that array. I am trying to figure out how to use JQ to export the complete datasets where object_type="deckCard" in each dataset of the array. More on reddit.com
🌐 r/bash
8
3
October 31, 2023
json - How to filter array of objects by element property values using jq? - Stack Overflow
1 Can jq check each element of a comma seperated array of values to check if the value exists in JSON? 0 Select or exclude multiples object with an array of IDs · 0 How to filter JSON array using JQ in shell (STRIPE)? More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
gist.github.com › deepns › 5b28ac586c31cf9d3cd8e4aedbbc425b
Some examples of filtering JSON data using jq · GitHub
.parent.child - access child of a parent json value. Equivalent to parent[child] syntax · Arrays are accessed using [] operator · .[] - access all items in the array (e.g. input | jq '.items[]') .[i] - index object at index i(e.g. input | jq '.items[1].name') .[i:j] - slice the array between index i and j. ~ % cat stackexchange_sites | jq '.items[1].name' "Server Fault" Filters can be combined using pipe operator |. Filter expressions are separated by space.
🌐
GitHub
gist.github.com › ipbastola › 2c955d8bf2e96f9b1077b15f995bdae3
JQ to filter JSON by value · GitHub
Remember: if JSON value has no double quotes (eg. for numeric) to do not supply in filter i.e. in contains(611) Copy link · if searching for exact value don't use contains as that is substring/array lookup ) use == i.e. cat my.json | jq -c '.[] | select( ._id == 611 ) And if your input is not a large array, but rather individual lines of json then use ·
🌐
jq
jqlang.org › manual
jq 1.8 Manual
If combined with --slurp, then the entire input is passed to the filter as a single long string. ... Instead of running the filter for each JSON object in the input, read the entire input stream into a large array and run the filter just once.
Find elsewhere
🌐
DEV Community
dev.to › anks › using-jq-to-filter-json-data-36c5
Using jq to filter JSON data - DEV Community
October 7, 2022 - [] [] - Select array, [n] - Select nth element in the array, [.key1, .key2, ..] - Create new json object · {} {key1: .key1, key2: .key2, ..} or {key1, key2, ..} - Create new json object ... $ jq '.[] | select(.id == 1) | .name' file.json "test1" $ cat file.json | jq '.[] | select(.id == 1) | .name' "test1"
🌐
Technitribe
blog.lnx.cx › 2019 › 09 › 09 › using-jq-to-filter-an-array-of-objects-from-json.html
Using jq to filter an array of objects from JSON | Technitribe
September 9, 2019 - $ jq '.prefixes | map(. | select(.service=="AMAZON"))' < ip-ranges.json | head [ { "ip_prefix": "18.208.0.0/13", "region": "us-east-1", "service": "AMAZON" }, Now we are getting each object returned as a member of an array. The difference is that we're putting the .prefixes array objects into the map function and telling it to iterate every object through the select function.
🌐
Reddit
reddit.com › r/bash › jq - filter datasets in array based on index value within each dataset of array.
r/bash on Reddit: JQ - filter datasets in array based on index value within each dataset of array.
October 31, 2023 - I managed to process it by spliting the log file in small part (one line = one json): ... Thanks. This is a nice trick to remember. ... Using JQ to return the index number where an element in an array has a specific value.
🌐
HayaGeek
hayageek.com › home › json manipulation with jq filter – examples
JSON Manipulation with jq Filter - Examples
November 17, 2023 - To get a list of values from an array of objects, you can use ‘.[] | .property_name’. ... To navigate through a nested array, combine array filters and object filters. ... For more complex data manipulation, combine and chain jq filters as needed. jq '.users[] | select(.age > 21).name' sample.json
Top answer
1 of 2
2

hmm, i managed to do it with this:

.[] | select(.foo as $tmpvar | ["aaa", "bbb"] | index ($tmpvar ) )

https://jqplay.org/s/g7AyRgARdU

According to this answer: https://stackoverflow.com/a/46470951/2244766 in versions above 1.5 there is a new IN operator that makes life a bit easier:

.[] | select(.foo|IN("aaa","bbb"))
2 of 2
0

The SQL-style operators don't work well for me as a straight-forward selection mechanism; I believe they have a very specific use-case, for which they are uniquely suitable for, and for anything else they're (at best) clunky. At least that's been my experience. And I haven't really figured out what that specific use-case is, either.

With all of that as a backdrop, my recommendation is to use a simple regex test:

map(select(.foo | test("aaa|bbb")))

Given the example JSON:

<~> $ jq . /tmp/so4229.json
[
  {
    "foo": "aaa",
    "bar": 111
  },
  {
    "foo": "bbb",
    "bar": 111
  },
  {
    "foo": "ccc",
    "bar": 222
  },
  {
    "foo": "aaa",
    "bar": 333
  },
  {
    "foo": "ddd",
    "bar": 444
  }
]

the above filter would result in:

<~> $ jq 'map(select(.foo | test("aaa|bbb")))' /tmp/so4229.json
[
  {
    "foo": "aaa",
    "bar": 111
  },
  {
    "foo": "bbb",
    "bar": 111
  },
  {
    "foo": "aaa",
    "bar": 333
  }
]

If you need to generate the regex based on other data within the JSON, you can do that, too:

. as $data | map(select(.bar==111) | .foo) | join("|") as $regex | . = $data | map(select(.foo | test($regex)))

which would result in:

<~> $ jq '. as $data | map(select(.bar==111) | .foo) | join("|") as $regex | . = $data | map(select(.foo | test($regex)))' /tmp/so4229.json
[
  {
    "foo": "aaa",
    "bar": 111
  },
  {
    "foo": "bbb",
    "bar": 111
  },
  {
    "foo": "aaa",
    "bar": 333
  }
]

There may be a better way to run through the JSON twice (once to get the regex values, once to use it).

🌐
Programming Historian
programminghistorian.org › en › lessons › json-and-jq
Reshaping JSON with jq | Programming Historian
May 24, 2016 - Keeping “Compact Output” checked, remove the [] from the filter, so it just reads .artObjects again. The results should now be just one line, as jq is now just returning one single JSON array: When jq returns just one JSON object, the ‘Compact Output’ option will produce a one-line result. If you want to access just the first (or the n-th) item in an array, put a digit in the [] operator: ... IMPORTANT: you access the first element of an array with 0, not 1.
🌐
Earthly
earthly.dev › blog › jq-select
JQ Select Explained: Selecting elements from JSON with Examples - Earthly Blog
July 24, 2023 - You can see this by explicitly asking jq to ignore its input and instead return two numbers: ... You can resolve this the same way you would turn the text 1,2 into an array in JavaScript: By wrapping it in an array constructor [ ... ]. ... Similarly, to put a generated collection of results into a JSON array, you wrap it in an array constructor [ ... ]. My GitHub issue title filter (.[].title) then becomes [ .[].title ] like this:
🌐
Baeldung
baeldung.com › home › web › guide to linux jq command for json processing
Guide to Linux jq Command for JSON Processing | Baeldung on Linux
August 31, 2025 - First, we iterate over the array ... the next filter in the command using a pipe |. The last step is to output the name field from each object using .name: ... We can also use a slightly more concise version and access the property directly on each object in the array: ... Of course, as with all arrays, we can access one of the items in the array directly by passing the index: ... Finally, jq also supports ...