Very close! In your select expression, you have to use a pipe (|) before contains.

This filter produces the expected output.

. - map(select(.Names[] | contains ("data"))) | .[] .Id

The jq Cookbook has an example of the syntax.

Filter objects based on the contents of a key

E.g., I only want objects whose genre key contains "house".

$ json='[{"genre":"deep house"}, {"genre": "progressive house"}, {"genre": "dubstep"}]'
$ echo "$json" | jq -c '.[] | select(.genre | contains("house"))'
{"genre":"deep house"}
{"genre":"progressive house"}

Colin D asks how to preserve the JSON structure of the array, so that the final output is a single JSON array rather than a stream of JSON objects.

The simplest way is to wrap the whole expression in an array constructor:

$ echo "$json" | jq -c '[ .[] | select( .genre | contains("house")) ]'
[{"genre":"deep house"},{"genre":"progressive house"}]

You can also use the map function:

$ echo "$json" | jq -c 'map(select(.genre | contains("house")))'
[{"genre":"deep house"},{"genre":"progressive house"}]

map unpacks the input array, applies the filter to every element, and creates a new array. In other words, map(f) is equivalent to [.[]|f].

Answer from Iain Samuel McLean Elder on Stack Overflow
🌐
GitHub
gist.github.com › ipbastola › 2c955d8bf2e96f9b1077b15f995bdae3
JQ to filter JSON by value · GitHub
I'm answering my own question. It is possible by piping one select statement into another for an AND operation. Not sure about the OR. You can also use or and and within select. cat my.json | jq -c '.[] | select( ._id == 611 or .author == "John Doe" )'
Discussions

JQ - filter datasets in array based on index value within each dataset of array.
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. ... React hook form: Registering fields within nested arrays while only rendering one child array at a time in form data output preview ... Using JQ to return the index number where an element in an array has a specific value... More on reddit.com
🌐 r/bash
8
3
October 31, 2023
json - jq: how to filter by field value in array of objects? - Stack Overflow
The relevant jq filter here is IN, not in. ... Even if your jq does not have IN, you can still use it by first adding its def, which is easily found e.g. More on stackoverflow.com
🌐 stackoverflow.com
How do I make remove all the extra spaces while using jq to read raw html into a JSON string
Try jq --rawfile html <( sed 's/(\w)\+/\1/g' ) The syntax <( ) enables you to provide a subprocess where a filename is expected. My sed command is an off-the-cuff attempt to replace repeating occurrences of whitespace with a single instance. You can use any command you want to clean up your html template. More on reddit.com
🌐 r/bash
2
2
August 10, 2023
Using JQ to return the index number where an element in an array has a specific value.
jq '[.Channels[].Channel_Short]|index("ch02")' Settings.json ? More on reddit.com
🌐 r/bash
8
7
July 27, 2023
🌐
jq
jqlang.org › manual
jq 1.8 Manual
The absolute simplest filter is . . This filter takes its input and produces the same value as output. That is, this is the identity operator. Since jq by default pretty-prints all output, a trivial program consisting of nothing but .
🌐
SANS
isc.sans.edu › diary › 27792
Filter JSON Data by Value with Linux jq - SANS ISC
cat mal_url.json | jq '.objects[].value + ": " + .objects[].source + ": " + .objects[].threat_type' | sort | uniq
🌐
DEV Community
dev.to › anks › using-jq-to-filter-json-data-36c5
Using jq to filter JSON data - DEV Community
October 7, 2022 - To filter ids: $ jq '.[].id' file.json 1 2 $ cat file.json | jq '.[] | .id' 1 2 $ cat file.json | jq '.[].id' 1 2 · To return value of name key when id is 1 · $ jq '.[] | select(.id == 1) | .name' file.json "test1" $ cat file.json | jq '.[] | select(.id == 1) | .name' "test1" To filter ids as json ·
🌐
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. ... React hook form: Registering fields within nested arrays while only rendering one child array at a time in form data output preview ... Using JQ to return the index number where an element in an array has a specific value...
Find elsewhere
🌐
Medium
kannappanchidambaram.medium.com › understanding-jq-a-guide-to-processing-and-filtering-json-data-2af8b5ec84cf
Understanding jq: A Guide to Processing and Filtering JSON Data | by Kannappan Chidambaram | Medium
December 3, 2023 - From this JSON if we need to fetch fields from the data array then the filter we want to use is called array/object value iterator .[]. This filter extracts values from the given array (or object), so the next filter in the pipeline gets applied ...
🌐
Container Solutions
blog.container-solutions.com › learn-jq-the-hard-way-part-iii-filters
Learn JQ the Hard Way, Part III - Filters
November 15, 2023 - Let’s say we want to know what user2‘s value is in this document. To do this, we need a filter that outputs only the value for a specific name. First, we can select the object, and then select by name: ... This is our first proper query with jq.
🌐
GitHub
gist.github.com › deepns › 5b28ac586c31cf9d3cd8e4aedbbc425b
Some examples of filtering JSON data using jq · GitHub
~ % cat stackexchange_sites | jq --raw-output '.items[] | .name' | sort | grep "^S" Seasoned Advice Seasoned Advice Meta Server Fault Stack Overflow Super User · We can also transform one json stream into another by specifying a filter with structure in { key : value} where value is the object ...
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).

🌐
GitHub
github.com › jbranchaud › til › blob › master › jq › filter-out-results-based-on-list-of-values.md
til/jq/filter-out-results-based-on-list-of-values.md at master · jbranchaud/til
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.
Author   jbranchaud
🌐
Zendesk Developer Docs
developer.zendesk.com › documentation › integration-services › developer-guide › jq-cheat-sheet
jq cheat sheet | Zendesk Developer Docs
Important: The expr parameter requires a valid JSON string value. Before using a jq expression in the expr parameter, escape any JSON special characters with a leading backslash (\). For example, use \" to escape double quotes. Disclaimer: Zendesk provides this article for instructional purposes only. Zendesk does not support or guarantee the jq expressions in this article. For complete jq documentation, refer to the jq manual. A jq expression consists one or more filters...
🌐
Earthly
earthly.dev › blog › jq-select
JQ Select Explained: Selecting elements from JSON with Examples - Earthly Blog
July 24, 2023 - Which is the same syntax for an object in a JSON document, except with jq you can use filters as values.4 · The next problem I have is that I want to summarize some this JSON data. Each issue returned by GitHub has a collection of labels:
🌐
DevDocs
devdocs.io › jq
DevDocs — jq documentation
jq 1.7 API documentation with instant search, offline support, keyboard shortcuts, mobile version, and more.
🌐
Phpfog
phpfog.com › home › blog › how to filter json records by value with jq
How to filter JSON records by value with jq - PHPFog.com
January 29, 2021 - jq offers an easy way to filter JSON records based on field values with the select() function.
🌐
DevTools Daily
devtoolsdaily.com › jq › examples › select-by-attribute-value
JQ Example: select by attribute value
JQ Examples · /select by attribute value · Ask AI · Your JSON content · CheatsheetHelp · Expression ·
🌐
iO Flood
ioflood.com › blog › jq-select
The jq 'select' Command | Your Key to JSON Data Filtering
November 16, 2023 - In this example, we’re using the jq ‘select’ command to filter a JSON object. We’re asking jq ‘select’ to return the JSON object if the ‘city’ value is “New York”. As the ‘city’ value in our JSON object is “New York”, the entire JSON object is returned. This basic use of jq ‘select’ is straightforward, but it’s incredibly powerful. By specifying different conditions, you can filter your JSON data to find exactly what you’re looking for.
🌐
DevOps.dev
blog.devops.dev › json-filter-with-jq-f7a7b42218a4
JSON filter with jq. Beginners article to start analyzing… | by Charles Vissol | DevOps.dev
October 14, 2024 - Here the filter walks through 2 keys. debug is the child key of widget. The output shows on value of the debug tag. This is a short article for beginners who wants to ease their JSON analysis without complex tools. One point to know is that jq works very well with large and complex JSON files.