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
🌐
jq
jqlang.org › manual
jq 1.8 Manual
The ordering for objects is a little ... keys (as arrays in sorted order), and if their keys are equal then the values are compared key by key. sort_by may be used to sort by a particular field of an object, or by applying any jq filter. sort_by(f) ...
Discussions

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
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. ... Find the answer to your question by asking. More on unix.stackexchange.com
🌐 unix.stackexchange.com
February 17, 2022
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
iteration - Output specific key value in object for each element in array with jq for JSON - Stack Overflow
I would like to loop through each object in this array, and pick out the value of each key called AssetId and output it. How would I do this using jq for the command line? More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
gist.github.com › olih › f7437fb6962fb3ee9fe95bda8d2c8fa4
jq Cheet Sheet · GitHub
Indeed, it works in jq version 1.7.1. Help much appreciated! ... to_entries[] | [ .value.chrom, .value.pos, (.value.alleles | keys[0]), (.value.alleles | keys[1:] | join(",")) ] | @tsv ... Seems you guys are expert. I can't find my way to do what I want. I have this json array:
🌐
GitHub
github.com › jqlang › jq › issues › 370
selecting an array element, based on subelement value -> jq: error: Cannot index array with string · Issue #370 · jqlang/jq
I can easily select a specific element in the sequence with · | jq '.DATA | .[2]' { "TYPE": "slave", "AXFR_IPS": "none", "DOMAIN": "domain2.net", "TTL_SEC": 0, "SOA_EMAIL": "", "DOMAINID": 22222, "DESCRIPTION": "", "EXPIRE_SEC": 0, "RETRY_SEC": 0, "STATUS": 1, "LPM_DISPLAYGROUP": "", "REFRESH_SEC": 0 },
Published   May 22, 2014
Author   ghost
🌐
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.

🌐
Zendesk Developer Docs
developer.zendesk.com › documentation › integration-services › developer-guide › jq-cheat-sheet
jq cheat sheet | Zendesk Developer Docs
The following expression extracts the first element from the follower_ids array. ... Combine the array index and object identifier-index filters to get a property from an array of objects. The following expression gets the id of the second object in the custom_fields array.
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 - Keyoxide · May 2, 2023 less than 1 minute read · Assuming you have a json string or file containing an array such as: { "response": [ 1000, { "text": "blabla" }, { "text": "blabla2" }, { "text": "blabla3" } ] } 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 ·
🌐
Earthly
earthly.dev › blog › jq-select
JQ Select Explained: Selecting elements from JSON with Examples - Earthly Blog
July 24, 2023 - It’s all just composing together filters until you get the result you need. Now lets talk about how you can put this knowledge into practice. ... jq lets you select elements by starting with a . and accessing keys and arrays like it’s a JavaScript Object (which is it is).
🌐
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!

🌐
Exercism
exercism.org › tracks › jq › concepts › arrays
Arrays in jq on Exercism
Ruby has a handy each_with_index method that pairs the element value with its index. Python has enumerate. jq has something similar, named to_entries. This transforms the array to an array of {key: index, value: value} objects.
🌐
Cameronnokes
cameronnokes.com › blog › jq-cheatsheet
JQ cheatsheet - Cameron Nokes
August 5, 2020 - jq -r '[(.dependencies, .devDependencies) | keys] | flatten | length' package.json · Gets an array of unique values. ... Joins the elements of an array using a separator.