Your statement does not work, because you try to feed the data object into match, but match can only work on strings.

The following expression will do what you want. The to_entries converts the object to an array of keys and values. Then we iterate over this array by using map and select all entries where the .key (now a string) has a match. Finally we just print out the value of every element.

.data | to_entries | map(select(.key | match("what a burger";"i"))) | map(.value)

However, two comments:

  • The [a,a,3] is not allowed in JSON, because a is not a number.
  • It works because the keys ARE actually different, even if only the letter case is not equal. If at least two keys are identical, you will run into problems, because keys should be unique. In fact, jq will only output one of the elements then.
Answer from koalo on Stack Overflow
🌐
jq
jqlang.org › manual
jq 1.8 Manual
Emit the string obtained by replacing the first match of regex in the input string with tostring, after interpolation. tostring should be a jq string or a stream of such strings, each of which may contain references to named captures.
Top page
jq is a lightweight and flexible command-line JSON processor
Download
jq is a lightweight and flexible command-line JSON processor
Tutorial
jq is a lightweight and flexible command-line JSON processor
Discussions

bash - Match keys with regex in jq - Unix & Linux Stack Exchange
I want to use jq tools, and check the values of key11, key12, key13 like this: ... If a matching key has the value null, then my script should exit 0. More on unix.stackexchange.com
🌐 unix.stackexchange.com
jq - Matching When Value Is List - Stack Overflow
As explained I am using jq, but the goal was matching exactly for each type, not all of them. Also, the version of jq I have does not understand "IN", probably compiled without it . I get "jq: error: IN/1 is not defined at , line 1" when running your suggestion. More on stackoverflow.com
🌐 stackoverflow.com
scripting - jq select match pattern but not if preceded by other pattern - Unix & Linux Stack Exchange
An application called "vagrant packer" always downloads the iso and checksum file from the remote location every time I run it, so I'm creating a script that downloads the iso to a local More on unix.stackexchange.com
🌐 unix.stackexchange.com
json - jq - select an attribute beginning with a string - Unix & Linux Stack Exchange
And if you need the output to be an array, then you should use map. Example: jq 'map( select(.hostname | startswith("abcd") ) )'. Then you can select the first match by simply using | first More on unix.stackexchange.com
🌐 unix.stackexchange.com
May 11, 2018
🌐
Zendesk Developer Docs
developer.zendesk.com › documentation › integration-services › developer-guide › jq-cheat-sheet
jq cheat sheet | Zendesk Developer Docs
The following expression uses the match filter to extract the first set of numbers from a string. Important: Escape any JSON special characters in the regex pattern. ... jq supports several date-related functions. The now function gets the current time in seconds since the Unix epoch.
🌐
Today I Learned
til.hashrocket.com › posts › uv0bjiokwk-use-jq-to-filter-objects-list-with-regex
Use jq to filter objects list with regex - Today I Learned
November 21, 2023 - My use case is filtering an array of objects that have an attribute that matches a particular regex and extract another attribute from that object. ... First get each element of an array. > data | jq '.[]' {name: 'Chris', id: 'aabbcc'} {name: 'Ryan', id: 'ddeeff'} {name: 'Ifu', id: 'aaddgg'}
🌐
Exercism
exercism.org › tracks › jq › concepts › regular-expressions
Regular Expressions in jq on Exercism
Regular expressions in jq are limited to a set of filters. When you need to know if a string matches a pattern, use the test filter.
🌐
Earthly
earthly.dev › blog › jq-select
JQ Select Explained: Selecting elements from JSON with Examples - Earthly Blog
July 24, 2023 - select is a built-in function that takes a boolean expression and only returns elements that match. It’s similar to the WHERE clause in a SQL statement or array filter in JavaScript. Like map, I find select comes up quite a bit, so while you may have to come back to this article or google it the first few times you need it, with luck, it will start to stick to your memory after that. ... curl https://api.github.com/repos/stedolan/jq/issues?per_page=100 | \ jq 'map({ title: .title, number: .number, labels: .labels | length }) | map(select(.labels > 0))'
Find elsewhere
🌐
Ubuntu Manpages
manpages.ubuntu.com › manpages › focal › man1 › jq.1.html
Ubuntu Manpage: jq - Command-line JSON processor
FLAGS is a string consisting of one of more of the supported flags: ○ g - Global search (find all matches, not just the first) ○ i - Case insensitive search ○ m - Multi line mode (´.´ will match newlines) ○ n - Ignore empty matches ○ p - Both s and m modes are enabled ○ s - Single line mode (´^´ -> ´\A´, ´$´ -> ´\Z´) ○ l - Find longest possible matches ○ x - Extended regex format (ignore whitespace and comments) To match whitespace in an x pattern use an escape such as \s, e.g. ○ test( "a\sb", "x" ). Note that certain flags may also be specified within REGEX, e.g. ○ jq -n ´("test", "TEst", "teST", "TEST") | test( "(?i)te(?-i)st" )´ evaluates to: true, true, false, false.
🌐
GitHub
github.com › jqlang › jq › issues › 223
Substring matching support for select function · Issue #223 · jqlang/jq
November 28, 2013 - How find objects with .message containing thor string ? The closest is select but it doesn't seem to support substring matching. (Example: jq 'select(.user == "thor" and .message contains .user)' or jq 'select(.message has "thor")'
Author   nareshv
Top answer
1 of 2
7

With

jq 'map_values(select(.value == "auto"))' file

... you pull out the parts of the top-level object that you are interested in:

{
  "package2": {
    "name": "package_2",
    "value": "auto"
  }
}

With map_values(expression), you apply expression to each sub-part of the input object. In this case, the part is kept if the test in the select() statement evaluates to true, and discarded otherwise. It's similar to map(expression), but you'd use map() on arrays and map_values() on objects.

From there, you can choose to get the top-level key:

$ jq -r 'map_values(select(.value == "auto"))|keys[]' file
package2

The keys function creates an array of all keys in the input object, and the [] at the end expands the array into a set of strings.

Note that if there are multiple sub-objects with auto as their .value key's value, you will get multiple strings out of this command.


For a brief moment, I was unsure whether you wanted the value of the .name key or the top-level key. Once I spotted that you only wanted the top-level key, I had already written the text below. I'm leaving it in as a sort of comment.

$ jq -r 'map_values(select(.value == "auto"))[].name' file
package_2

Using [].name at the end, expand the top-level object into a set of sub-objects and then extract the .name key's value from each.

This last one could also have been written

$ jq -r 'map_values(select(.value == "auto").name)[]' file
package_2

... which reduces the original object to only

{
  "package2": "package_2"
}

... and then extracts the values of all remaining keys with the trailing [].

2 of 2
3

You can use jq's select() function:

jq -r '.[] | select(.value=="auto").name'

Also your json example is currently invalid.

🌐
Debian Manpages
manpages.debian.org › testing › jq › jq.1.en.html
jq(1) — jq — Debian testing — Debian Manpages
Splits an input string on each regex match. For backwards compatibility, when called with a single argument, split splits on a string, not a regex. jq ´split(", *"; null)´ "ab,cd, ef" => ["ab","cd","ef"]