The -r option should not be used if the output will be fed into another JSON parser; only the last jq in the pipeline should use it – otherwise you risk feeding non-JSON data into the second jq instance.

Your last example with .[0].t seems correct and works with jq 1.6.

$ cat json | jq '.tx' | jq -r '.[0].t'
t for a

But this can be simplified to a single jq invocation, first incrementally to:

$ cat json | jq -r '.tx | .[0].t'
t for a

and then finally to:

$ cat json | jq -r '.tx[0].t'
t for a
Answer from grawity on Stack Exchange
🌐
Zendesk Developer Docs
developer.zendesk.com › documentation › integration-services › developer-guide › jq-cheat-sheet
jq cheat sheet | Zendesk Developer Docs
A jq expression consists one or more filters. Use an object identifier-index filter to get properties from a JSON object. The following expression extracts a ticket id from a nested ticket object. ... Use an array index filter to get elements from a JSON array.
Discussions

json - Select objects based on value of variable in object using jq - Stack Overflow
I am using jq and want to get the "name" elements of the objects where 'location' is 'Stockholm'. ... But I can't figure out how to print only certain objects, given the value of a sub key (here: "location" : "Stockholm"). More on stackoverflow.com
🌐 stackoverflow.com
jq: Extract element from object or array of objects
The shortest solution that comes to mind: jq '[..| objects | .number | select(. != null)]' phones.json which is a basic 4-element pipeline: recursive descent selecting only objects extracting their number value ignoring nulls (from generated objects that don't contain a `number field) Wrap it all up in an array, and Bob's your uncle. More on reddit.com
🌐 r/commandline
6
3
May 10, 2025
shell - Get value of JSON [] array in jq - Unix & Linux Stack Exchange
mykey=1645128900000 jq --argjson key "$mykey" '.[] | select(first == $key) | last' file · This selects all the array entries with the given key as its first element and then extracts the value, the last element, from each piece chosen. More on unix.stackexchange.com
🌐 unix.stackexchange.com
February 17, 2022
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
🌐
GitHub
gist.github.com › olih › f7437fb6962fb3ee9fe95bda8d2c8fa4
jq Cheet Sheet · GitHub
cat example.json | jq '.. | to_entries? | [.key , .value] | join("=")' Thanks for any help! ... Did you check this StackOverflow question? It seems to match what you want. ... @bfontaine - thank you! This helps tremendously. This accomplishes the flattening aspect quite well, I can tweak to get formatting/output from here.
🌐
Earthly
earthly.dev › blog › jq-select
JQ Select Explained: Selecting elements from JSON with Examples - Earthly Blog
July 24, 2023 - jq lets us treat the JSON document as an object and select elements inside of it. Here is how I filter the JSON document to select the value of the name key:
🌐
Reddit
reddit.com › r/commandline › jq: extract element from object or array of objects
r/commandline on Reddit: jq: Extract element from object or array of objects
May 10, 2025 -

Given the following JSON, what is the best way to extract the phone numbers, whether inside an object or an array of objects?

{
  "phones": {
    "Alex Baker": { "location": "mobile", "number": "+14157459038" },
    "Bob Clarke": [
      { "location": "mobile", "number": "+12135637813" },
      { "location": "office", "number": "+13104443200" }
    ],
    "Carl Davies": [
      { "location": "office", "number": "+14083078372" },
      { "location": "lab", "number": "+15102340052" }
    ],
    "Drew Easton": { "location": "office", "number": "+18057459038" }
  }
}

I'm using the following query, but I wonder if there's a better way to do this:

$ cat phones.json | jq '.phones | to_entries | [ .[].value | objects | .number ] + [ .[].value | arrays | .[].number ]'
[
  "+14157459038",
  "+18057459038",
  "+12135637813",
  "+13104443200",
  "+14083078372",
  "+15102340052"
]

Any suggestions will be appreciated, thanks!

Find elsewhere
🌐
Proinsias
proinsias.github.io › til › jq-getting-all-the-values-of-an-array
Jq: Getting all the values of an array - Looking for data in all the right places…
May 13, 2025 - You can extract the values of the text field from the array using jq: > jq '.response[].text?' file.json "blabla" "blabla2" "blabla3" Or you can select using the type of each array element using either jq '.response[] | objects | .text' file.json or jq '.response[] | select(type=="object" and has("text")) | .text' file.json ·
🌐
jq
jqlang.org › manual
jq 1.8 Manual
So, the expression .[] | .foo retrieves the "foo" field of each element of the input array. This is a cartesian product, which can be surprising. Note that .a.b.c is the same as .a | .b | .c. Note too that . is the input value at the particular stage in a "pipeline", specifically: where the . expression appears. Thus .a | . | .b is the same as .a.b, as the . in the middle refers to whatever value .a produced. ... Parenthesis work as a grouping operator just as in any typical programming language. ... jq supports the same set of datatypes as JSON - numbers, strings, booleans, arrays, objects (which in JSON-speak are hashes with only string keys), and "null".
🌐
Reddit
reddit.com › r/bash › using jq to return the index number where an element in an array has a specific value.
r/bash on Reddit: Using JQ to return the index number where an element in an array has a specific value.
July 27, 2023 -

I managed to do this when I was teaching myself JSON and playing with JQ. Now I can't remember how I did this. So any guidance would be of value.

Take the following file: settings.json

{
  "Bot_API_Key": "SuperSecretKey",
  "Channels": [
    {
      "Channel_Name": "First Channel",
      "Channel_Short": "ch01",
      "Channel_ID": 4004841050681
    },
    {
      "Channel_Name": "Second Channel",
      "Channel_Short": "ch02",
      "Channel_ID": 4004685917007
    }
  ]
}

If I use the following:

jq '.Channels[] | contains("ch02")' settings.json

It returns:

false
true

What I actually need is the index number in the array. In this case it must return 1.

I did achieve this once, a few weeks back, when experimenting, and now I can't repeat the results. Like a fool, I didn't document everything I did.

🌐
DevDocs
devdocs.io › jq
DevDocs — jq documentation
jq 1.7 API documentation with instant search, offline support, keyboard shortcuts, mobile version, and more.
🌐
Exercism
exercism.org › tracks › jq › concepts › arrays
Arrays in jq on Exercism
Retrieve an element from an array with a bracket expression: .[2] gets the third element.
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.

🌐
jQuery
api.jquery.com › val
.val() | jQuery API Documentation
The .val() method is primarily used to get the values of form elements such as input, select and textarea. When called on an empty collection, it returns undefined. When the first element in the collection is a select-multiple (i.e., a select element with the multiple attribute set), .val() ...
🌐
Super User
superuser.com › questions › 1750402 › extract-element-from-json-array-in-file-with-jq
bash - Extract element from JSON array in file with jq - Super User
Ignoring the broken JSON (no comma allowed on the last key:value pair) and that the Name does not equal "bbb" (there's a space): jq -r 'map(select(.Name == " bbb")) | first | .ReferenceId' list.json