Use slurp mode:

  o   --slurp/-s:

      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.
$ jq -s '.' < tmp.json
[
  {
    "name": "John",
    "email": "[email protected]"
  },
  {
    "name": "Brad",
    "email": "[email protected]"
  }
]
Answer from chepner on Stack Overflow
🌐
jq
jqlang.org › manual
jq 1.8 Manual
This option passes a JSON-encoded value to the jq program as a predefined variable. If you run jq with --argjson foo 123, then $foo is available in the program and has the value 123.
Top page
After a five-year hiatus, we've ... thanks to our new admins and maintainers. Check out the download page for installation options and see the release notes for details. ... jq 1.5 released, including new datetime, math, and regexp functions, try/catch syntax, array and object ...
Download
jq is a lightweight and flexible command-line JSON processor
Tutorial
If you want to get the output as a single array, you can tell jq to "collect" all of the answers by wrapping the filter in square brackets:
Discussions

Creating an array from objects?
First of all, kudos on such an ... used jq for basic CLI tasks and have only recently delved into its more advanced functions, and am continually amazed at how things just work with few surprises...rare for a CLI tool that has so many features... So I think my question is pretty basic, and I'm missing something very obvious that could be clarified in the docs. Given a series of objects, what do I pipe them through to get them into an array... More on github.com
🌐 github.com
7
January 29, 2015
More fun with jq, getting results into a usable array
It's better practice to not chain together results within $() or backticks. Unless you're expecting a huge volume of data returned from your curl calls, it's better to capture it's result directly, so you can explicitly check for failure of the curl call itself, and failure of the request. If it looks valid, then proceed to parse it as you wish. It looks like you might not need to put your fields into arrays. You might be able to use jq -c to iterate through your initial curl call result, and then pull the values you need into separate variables, which you can then use to make the next requests. Something like: foo=$( curl something ) || { echo >&2 "curl failed"; exit 1; } # check $foo for http errors if $foo invalid; then exit 1; fi json_from_foo=$( filter_result "$foo" ) || { echo >&2 "failed to extract json from result"; exit 1; } for json_record in $( jq -c '.[]' <<< "$json_from_foo); do action=$(jq -r '.action' <<< $json_record) expr=$(jq -r '.expression' <<< $json_record) desc=$(jq -r '.description' <<< $json_record) process_record "$action" "$expr" "$desc" done More on reddit.com
🌐 r/bash
2
2
August 17, 2024
How to use jq to convert an bash array in command line to json array? - Unix & Linux Stack Exchange
$ jq -c -n -e '[$x, $y]' --argjson x '"a"' --argjson y '"b"' ["a","b"] I know that I could do something like the above. If I want to generate a json array f... More on unix.stackexchange.com
🌐 unix.stackexchange.com
December 15, 2022
bash - Add JSON objects to array using jq - Unix & Linux Stack Exchange
My goal is to output a JSON object using jq on the output of a find command in bash. It could either be a one-line command or a bash script. I have this command which creates JSON objects from eac... More on unix.stackexchange.com
🌐 unix.stackexchange.com
February 26, 2020
🌐
Zendesk Developer Docs
developer.zendesk.com › documentation › integration-services › developer-guide › jq-cheat-sheet
jq cheat sheet | Zendesk Developer Docs
The following expression combines the collaborator_ids and submitter_id properties into a single array. ... Use the index filter to get the index of the first occurrence of an element in an array. If the element doesn't exist in the array, the filter returns a null value.
🌐
GitHub
github.com › jqlang › jq › issues › 684
Creating an array from objects? · Issue #684 · jqlang/jq
January 29, 2015 - First of all, kudos on such an ... used jq for basic CLI tasks and have only recently delved into its more advanced functions, and am continually amazed at how things just work with few surprises...rare for a CLI tool that has so many features... So I think my question is pretty basic, and I'm missing something very obvious that could be clarified in the docs. Given a series of objects, what do I pipe them through to get them into an array...
Author   dannguyen
🌐
Reddit
reddit.com › r/bash › more fun with jq, getting results into a usable array
r/bash on Reddit: More fun with jq, getting results into a usable array
August 17, 2024 -

I'm using this in a cURL to get the data from result[]:

foo=$(curl --request GET \
--silent \
--url https://example.com \
--header 'Content-Type: application/json' | jq -r '.result[]')

When I print $foo, this is what I have:

[key]
default

firewall_custom
zone
34
[
  {
    "id": "example",
    "version": "6",
    "action": "block",
    "expression": "lorem",
    "description": "ipsum",
    "last_updated": "2024-08-15T19:10:24.913784Z",
    "ref": "example",
    "enabled": true
  },
  {
    "id": "example2",
    "version": "7",
    "action": "block",
    "expression": "this",
    "description": "that",
    "last_updated": "2024-08-15T19:10:24.913784Z",
    "ref": "example2",
    "enabled": true
  }
]

What I need from this is to create a loop where, in a series of addtional cURLs, I can insert action, expression, and description.

I'm imagining that I would push these to 3 separate arrays (action, expression, and description), so that ${action[0]} would coincide with ${expression[0]} and ${description[0]}, and so on.

Something along the lines of:

# assuming that I have somehow created the following arrays:
# action=("block" "block")
# expression=("lorem" "this")
# description=("ipsum" "that")

for x in ${action[@]}; do
  bar=$(curl --request GET \
    --silent \
    --url https://example.com \
    --data '{
      "action": ${action[$x]},
      "expression": ${expression[$x]},
      "description": ${description[$x]}
    }' | jq '.success')

  if [[ $bar == true ]]
    then
      printf "$x succeeded\n"

    else
      printf "$x failed\n"
  fi

  # reset bar
  bar=''
done

The question is, how to create action, expression, and description arrays from the results of $foo (that original cURL)?

🌐
Programming Historian
programminghistorian.org › en › lessons › json-and-jq
Reshaping JSON with jq | Programming Historian
May 24, 2016 - The results should now be just one line, as jq is now just returning one single JSON array: When jq returns just one JSON object, the ‘Compact Output’ option will produce a one-line result. If you want to access just the first (or the n-th) item in an array, put a digit in the [] operator:
Find elsewhere
🌐
Exercism
exercism.org › tracks › jq › concepts › arrays
Arrays in jq on Exercism
Ready to put your newfound language skills into use? Get 40% off CodeCrafter's real-world proficiency projects! Claim your discount · Tracks · / jq · / Syllabus · / Arrays · Ar · 5 exercises · JSON defines an array as: An array is an ordered collection of values.
🌐
iO Flood
ioflood.com › blog › jq-array
Manipulating JSON Arrays with jq | Example Guide
November 15, 2023 - Creating an array in jq is straightforward. Let’s start with a simple example: echo '[]' | jq '. += ["Apple", "Banana", "Cherry"]' # Output: # ["Apple", "Banana", "Cherry"] In this example, we use the echo command to create an empty array, and then we use jq to add three elements to it: “Apple”, “Banana”, and “Cherry”. The '. += ["Apple", "Banana", "Cherry"]' part of the command is jq’s syntax for adding elements to an array.
🌐
jq
jqlang.org › tutorial
Tutorial
If you want to get the output as a single array, you can tell jq to "collect" all of the answers by wrapping the filter in square brackets:
🌐
jq recipes
remysharp.com › drafts › jq-recipes
jq recipes
April 16, 2024 - echo "1\n2\n3" | jq --slurp --raw-input 'split("\n")[:-1]' ... Convert a plain list of timestamps to an array of objects with date and time separated (using jq's --slurp and --raw-input options combined):
🌐
Eliatra
eliatra.com › home › blog › transform json data on bash using jq
Transform JSON Data on Bash Using jq
November 23, 2022 - The output of this command is our original JSON array of objects but with the incremented “EmployedSince” field. Next, we pipe this output of jq back to jq, and tell it (again) that we are working with an arrays and want to extract the fields LastName, FirstName, and EmployedSince.
🌐
Baeldung
baeldung.com › home › files › file editing › how to add objects into json array using jq
How to Add Objects Into JSON Array Using jq | Baeldung on Linux
March 18, 2024 - In this tutorial, we’ll discuss how to add a JSON object to a JSON array in another file. For that purpose, we’ll make use of the jq utility.
🌐
Earthly
earthly.dev › blog › jq-select
An Introduction to JQ
July 24, 2023 - You can see this by explicitly asking jq to ignore its input and instead return two numbers: ... You can resolve this the same way you would turn the text 1,2 into an array in JavaScript: By wrapping it in an array constructor [ ...