jq can deal with multiple input arrays. You can pipe the whole output of the loop to it:

for service in "$services" ; do
    curl "$service/path"
done | jq -r '.[]|[.id,.startDate,.calls]|@csv'

Note that the csv transformation can be done by @csv

Answer from hek2mgl on Stack Overflow
🌐
jq
jqlang.org › manual
jq 1.8 Manual
def recurse(f): def r: ., (f | select(. != null) | r); r; def while(cond; update): def _while: if cond then ., (update | _while) else empty end; _while; def repeat(exp): def _repeat: exp, _repeat; _repeat; Some jq operators and functions are actually generators in that they can produce zero, one, or more values for each input, just as one might expect in other programming languages that have generators. For example, .[] generates all the values in its input (which must be an array or an object), range(0; 10) generates the integers between 0 and 10, and so on.
Discussions

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
bash - jq create array and append entry to it - Stack Overflow
Script is selfexplaining. Except jq part. I dont know how to handle jq to create json file. Basically I want to iterate over list of urls, populate 2 variables and push it to results.json file as entry/per iteration. ... not selfexplaining. Use parentheses to create a bash array: list=( url1 url2 ). More on stackoverflow.com
🌐 stackoverflow.com
May 29, 2018
jq - add objects from file into json array - Unix & Linux Stack Exchange
It will apply across a single or ... as an array. It's also possible to interleave things to process multiple orig files, each with one companion file inserted, but separating the outputs would be a hassle. ... But this probably doesn't let me incorporate non-json files. With --arg I can do: jq --arg "$(<1.txt)" ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
Creating an array from objects?
First of all, kudos on such an excellent library...I've used jq for basic CLI tasks and have only recently delved into its more advanced functions, and am continually amazed at how things just ... More on github.com
🌐 github.com
7
January 29, 2015
🌐
iO Flood
ioflood.com › blog › jq-array
Manipulating JSON Arrays with jq | Example Guide
November 15, 2023 - 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 ...
🌐
Exercism
exercism.org › tracks › jq › concepts › arrays
Arrays in jq on Exercism
The empty brackets can be placed beside the previous expression, omitting the pipe. ["a", "be", "cat", "door"][] | length # => 1, 2, 3, 4 -- the lengths of each element # ........................^^ Use + to concatenate arrays. [1, 2, 3] + [4, 5] # => [1, 2, 3, 4, 5] The - operator removes elements in the right-hand array from the left-hand array. [range(10)] - [2, 4, 6] # => [0, 1, 3, 5, 7, 8, 9] jq provides many functions to cover common iteration functionality: map(expr) returns a new array where the expr is applied to each element in turn.
Find elsewhere
🌐
Ditig
ditig.com › jq-recipes
jq JSON processor recipes & command examples - Ditig
February 11, 2021 - Makes use of to_entries, which converts an object to an array of key/value objects. $ echo ' { "theArray": [ { "key": "foo", "id": "0123" }, { "key": "bar", "id": "4567" } ] }' | \ jq '.theArray | to_entries | map( { (.value.id): .key} ) | add' ... Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/ jq Recipes by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
🌐
jq
jqlang.org › manual › v1.4
jq 1.4 Manual
A jq program is a "filter": it takes an input, and produces an output. There are a lot of builtin filters for extracting a particular field of an object, or converting a number to a string, or various other standard tasks. Filters can be combined in various ways - you can pipe the output of one filter into another filter, or collect the output of a filter into an array.
🌐
jq recipes
remysharp.com › drafts › jq-recipes
jq recipes
April 16, 2024 - Here's a collection of jq recipes I've collected over the last few months. ... I've published 38 videos for new developers, designers, UX, UI, product owners and anyone who needs to conquer the command line today. ... Read a plain list of strings from a file into an array, specifically splitting into an array and removing the last empty \n:
Top answer
1 of 4
24

jq has a flag for feeding actual JSON contents with its --argjson flag. What you need to do is, store the content of the first JSON file in a variable in jq's context and update it in the second JSON

jq --argjson groupInfo "$(<input.json)" '.[].groups += [$groupInfo]' orig.json

The part "$(<input.json)" is shell re-direction construct to output the contents of the file given and with the argument to --argjson it is stored in the variable groupInfo. Now you add it to the groups array in the actual filter part.

Putting it in another way, the above solution is equivalent of doing this

jq --argjson groupInfo '{"id": 9,"version": 0,"lastUpdTs": 1532371267968,"name": "Training" }' \
   '.[].groups += [$groupInfo]' orig.json
2 of 4
15

This is the exact case that the input function is for:

input and inputs [...] read from the same sources (e.g., stdin, files named on the command-line) as jq itself. These two builtins, and jq’s own reading actions, can be interleaved with each other.

That is, jq reads an object/value in from the file and executes the pipeline on it, and anywhere input appears the next input is read in and is used as the result of the function.

That means you can do:

jq '.[].groups += [input]' orig.json input.json

with exactly the command you've written already, plus input as the value. The input expression will evaluate to the (first) object read from the next file in the argument list, in this case the entire contents of input.json.

If you have multiple items to insert you can use inputs instead with the same meaning. It will apply across a single or multiple files from the command line equally, and [inputs] represents all the file bodies as an array.

It's also possible to interleave things to process multiple orig files, each with one companion file inserted, but separating the outputs would be a hassle.

🌐
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 excellent library...I've 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
🌐
Programming Historian
programminghistorian.org › en › lessons › json-and-jq
Reshaping JSON with jq | Programming Historian
May 24, 2016 - By wrapping . operators within either [] or {}, jq can synthesize new JSON arrays and objects. This can be useful if you want to output a new JSON file. As we will see below, this can also be a crucial intermediate step when reshaping complex JSON. Create a new set of JSON objects with the ...
🌐
Stack Overflow
stackoverflow.com › questions › 70828479 › is-it-impossible-to-create-an-empty-array-and-add-a-json-object-to-it
linux - is it impossible to create an empty array and add a json object to it? - Stack Overflow
January 24, 2022 - The (quite simplistic) answer to your your question "is it impossible to create an empty array and add a json object to it" would be jq -n --argjson obj '{…}' '[$obj]' but I think you should rephrase that as a new question (or edit this one) ...
🌐
Qmacro
qmacro.org › blog › posts › 2022 › 05 › 06 › converting-strings-to-objects-with-jq
Converting strings to objects with jq - DJ Adams
May 6, 2022 - In other words, they weren't separators, they were just part of the data, and so the last newline at the end of the last string "SAP-samples/iot-edge-samples" meant that split would produce a final empty value, i.e. what it found to the right of the last newline character, as we can see in the last array position above (""). Of course, I was tempted to munge the input data before even feeding it to jq, so each repository name would be a valid JSON value.
🌐
Oracle
docs.oracle.com › cd › E88353_01 › html › E37839 › jq-1.html
jq - man pages section 1: User Commands
July 27, 2022 - It's useful for filtering lists: [1,2,3] | map(select(. >= 2)) will give you [2,3]. jq 'map(select(. >= 2))' [1,5,3,0,7] => [5,3,7] jq '.[] | select(.id == "second")' [{"id": "first", "val": 1}, {"id": "second", "val": 2}] => {"id": "second", "val": 2} arrays, objects, iterables, booleans, numbers, normals, finites, strings, nulls, values, scalars These built-ins select only inputs that are arrays, objects, iterables (arrays or objects), booleans, numbers, normal numbers, finite numbers, strings, null, non-null values, and non-iterables, respectively. jq '.[]|numbers' [[],{},1,"foo",null,true,false] => 1 empty empty returns no results.