Adapted from this post on Processing JSON with jq, you can use the select(bool) like this:

$ jq '.[] | select(.location=="Stockholm")' json
{
  "location": "Stockholm",
  "name": "Walt"
}
{
  "location": "Stockholm",
  "name": "Donald"
}
Answer from Daniel on Stack Overflow
Discussions

json - jq select value from array - Stack Overflow
I'm also on the cat file.json side ... cleanup and filter commands all piped together and that jq is just the last part of a bigger equation. 2024-11-28T23:35:07.643Z+00:00 ... The question was about selecting a specific field based on value of other field.... More on stackoverflow.com
🌐 stackoverflow.com
Using jq, extract fields and subfields from a list of objects, grouping paired subfields for saving to csv - Unix & Linux Stack Exchange
Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI ... More on unix.stackexchange.com
🌐 unix.stackexchange.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
json - jq convert an array by selecting to a list using only some values of the objects in the array - Unix & Linux Stack Exchange
3 use jq to pick a key out of a list of a list of objects and raw output with newline separation for outer array items · 0 Extracting two (or more) related values from an array of JSON objects More on unix.stackexchange.com
🌐 unix.stackexchange.com
September 17, 2022
🌐
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.
Top answer
1 of 2
4

You want to run a .context,.score filter on each element of v I think:

$ jq -r '.[] | [.c, .e, .score, (.v[] | .context,.score)] | @csv' file.json
"A","B",0.99,"asdf",0.98,"bcdfd",0.97

This is equivalent to using the builtin map function without assembling the results back into an array.

2 of 2
2

The following creates a JSON-encoded CSV record for each top-level array element, and then extracts and decodes them. For each of the top-level elements, the values of the sub-array is incorporated by "flattening" the array.

jq -r 'map([ .c,.e,.score, (.v|map([.context, .score])) ] | flatten | @csv)[]' file

Given a test document equivalent of the following:

[
   {
      "c": "A",
      "e": "B",
      "score": 0.99,
      "v": [
         { "context": "asdf", "score": 0.98, "url": "..." },
         { "context": "bcdfd", "score": 0.97, "url": "..." }
      ]
   },
   {
      "c": "A",
      "e": "B",
      "score": 0.99,
      "v": [
         { "context": "asdf", "score": 0.98, "url": "..." },
         { "context": "asdf", "score": 0.98, "url": "..." },
         { "context": "bcdfd", "score": 0.97, "url": "..." }
      ]
   },
   {
      "c": "A",
      "e": "B",
      "score": 0.99,
      "v": [
         { "context": "asdf", "score": 0.98, "url": "..." },
         { "context": "asdf", "score": 0.98, "url": "..." },
         { "context": "asdf", "score": 0.98, "url": "..." },
         { "context": "bcdfd", "score": 0.97, "url": "..." }
      ]
   }
]

... we get

"A","B",0.99,"asdf",0.98,"bcdfd",0.97
"A","B",0.99,"asdf",0.98,"asdf",0.98,"bcdfd",0.97
"A","B",0.99,"asdf",0.98,"asdf",0.98,"asdf",0.98,"bcdfd",0.97

One could also reorder the operations so that a single use of the @csv operator gets a set of arrays (rather than repeatedly using @csv on single arrays):

jq -r 'map([ .c,.e,.score, (.v|map([.context, .score])) ] | flatten)[]|@csv' file
🌐
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!

🌐
GitHub
gist.github.com › olih › f7437fb6962fb3ee9fe95bda8d2c8fa4
jq Cheet Sheet · GitHub
-> % jq '[path(..)] | .[3] ' example.json [ "data", "object", "user" ] (ehsm-py3.9) cbongior@cbongior-mac [09:54:38] [~/dev/ehsm] [main *] -> % jq 'getpath([ "data", "object", "user" ])' example.json { "id": 1, "range": [ -255, 0, 255 ], "notation": "big-O", "details": { "lat": 0.000, "long": 0.000, "time": 42 } } (ehsm-py3.9) cbongior@cbongior-mac [09:55:03] [~/dev/ehsm] [main *] -> % jq '[path(..)] | .[3] | getpath' example.json jq: error: getpath/0 is not defined at <top-level>, line 1: [path(..)] | .[3] | getpath jq: 1 compile error (ehsm-py3.9) cbongior@cbongior-mac [09:55:25] [~/dev/ehsm
🌐
Earthly
earthly.dev › blog › jq-select
JQ Select Explained: Selecting elements from JSON with Examples - Earthly Blog
July 24, 2023 - jq lets you select the whole array [], a specific element [3], or ranges [2:5] and combine these with the object index if needed.
Find elsewhere
🌐
jq
jqlang.org › manual
jq 1.8 Manual
This is useful when using jq as a simple calculator or to construct JSON data from scratch. ... Don't parse the input as JSON. Instead, each line of text is passed to the filter as a string. 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.
🌐
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 ·
Top answer
1 of 1
5
jq '.fields |= ( map(.key = .name | del(.type,.linkedId,.name)) | from_entries )' file

This updates the fields array. First, for each element (using map()), a key key is created with the value from the name key. Then the type, linkedId, and name keys are deleted from the element. We are now left with a key and a value key in each element of the fields array, which is just what from_entries needs to associate the key values as keys to the value values.

Testing on the data given in the question (with a trailing comma removed), this generates the following JSON:

{
  "name": "Papanito",
  "fields": {
    "Name1": "Value1",
    "Name2": "Value2",
    "Name3": "Value3",
    "Name4": "Value3"
  }
}

Note that your expected output is invalid JSON, as an array can't have keys. Therefore, the fields value in the output above is an object, not an array.

Would you want to keep fields as an array, you could rearrange the operations a bit:

jq '.fields |= map([.key = .name | del(.type,.linkedId,.name)] | from_entries)' file

For the given data (corrected as mentioned above), this would generate something equivalent to the following document:

{
   "name": "Papanito",
   "fields": [
      { "Name1": "Value1" },
      { "Name2": "Value2" },
      { "Name3": "Value3" },
      { "Name4": "Value3" }
   ]
}

This has the benefit that two elements with the same original name values would not overwrite each other in the result.


Conceptually, the above deletes the data that we no longer want. The following variations extract the data we want instead.

This generates the last result from above:

jq '.fields |= map({ (.name): .value })' file

And we can get the first result (with fields as a single object) by merging the entries in the list:

jq '.fields |= (map({ (.name): .value }) | add)' file
🌐
Luis Cacho
luiscachog.io › garden › jq-select-based-on-value
jq select objects based on a value | Luis Cacho
May 18, 2022 - jq '.DATA[] | select(.DOMAIN == ("domain2","domain3")) | .DOMAINID,.DOMAIN' 22222 "slave" 33333 "slave" # or jq '.DATA[] | select(.DOMAIN | IN("domain2", "domain3")) | .DOMAINID,.DOMAIN' 22222 "slave" 33333 "slave"
🌐
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
🌐
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.
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.

🌐
Cameronnokes
cameronnokes.com › blog › jq-cheatsheet
JQ cheatsheet - Cameron Nokes
August 5, 2020 - echo '{ "firstName": "Cameron", "lastName": "Nokes" }' | jq '[.firstName, .lastName] | join(" ")' # Cameron Nokes · We create an array of the properties we want joined, pipe to join which is passed a space as the separator.