In jq 1.3 and up you can use the --arg VARIABLE VALUE command-line option:

jq -n --arg v "$VAR" '{"foo": $v}'

I.e., --arg sets a variable to the given value so you can then use $varname in your jq program, and now you don't have to use shell variable interpolation into your jq program.

EDIT: From jq 1.5 and up, you can use --argjson to pass in an array directly, e.g.

jq -n --argjson v '[1,2,3]' '{"foo": $v}'
Answer from user2259432 on Stack Overflow
🌐
Zendesk Developer Docs
developer.zendesk.com › documentation › integration-services › developer-guide › jq-cheat-sheet
jq cheat sheet | Zendesk Developer Docs
Important: Double quotes (") are a JSON special character. Before using this expression in the expr parameter, escape any double quotes with a leading backslash (\"). ... To create an array, wrap the output in square brackets ([]).
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
linux - creating a nested json file from variables using jq - Unix & Linux Stack Exchange
but my output desired output in the question has array braces after artifacts How do I create that ? ... The safest way to create JSON on the command line is through using a tool that constructs it for you as jq does. More on unix.stackexchange.com
🌐 unix.stackexchange.com
November 8, 2021
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? I've found I can do this: cat some.json | jq '.[] ... More on github.com
🌐 github.com
7
January 29, 2015
How to create array of json objects using jq and set to var in bash - Stack Overflow
I have like this variables in my bash script: server1_name = 'server-1' server1_url = 'http://server-1.net' server2_name = 'server-2' server2_url = 'http://server-2.net' How I can create like thi... More on stackoverflow.com
🌐 stackoverflow.com
August 26, 2021
🌐
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 ...
🌐
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? I've found I can do this: cat some.json | jq '.[] | { attr1, attr2 }' | jq --slurp '.'
Author   dannguyen
🌐
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 - $ jq '.posts += [inputs]' blog.json post-1.json post-2.json · Once executed, it produces a JSON with all the posts inside the array:
🌐
iO Flood
ioflood.com › blog › jq-array
Manipulating JSON Arrays with jq | Example Guide
November 15, 2023 - In this comprehensive guide, we’ve journeyed through the world of jq, a powerful command-line tool for manipulating JSON data, with a special focus on handling arrays. We started with the basics, learning how to create, access, and modify arrays using jq. We then ventured into more advanced territory, exploring complex operations like filtering, mapping, and reducing arrays.
Find elsewhere
Top answer
1 of 2
4

Pass your strings using --arg, then you can create the JSON as expected:

#!/bin/bash

server1_name='server-1'
server1_url='http://server-1.net'

server2_name='server-2'
server2_url='http://server-2.net'

result=$(jq -n \
    --arg name1 "$server1_name" \
    --arg url1 "$server1_url" \
    --arg name2 "$server2_name" \
    --arg url2 "$server2_url" \
    '[ { "name": $name1, "url": $url1 }, { "name": $name2, "url": $url2 } ]')

echo "$result"

Will produce:

[
  {
    "name": "server-1",
    "url": "http://server-1.net"
  },
  {
    "name": "server-2",
    "url": "http://server-2.net"
  }
]
2 of 2
1

You can construct two arrays of names and urls, then adapt this answer to "zip" the two arrays together into the desired array of objects.

jq -n \
    --arg name1 "$server1_name" \
    --arg url1 "$server1_url" \
    --arg name2 "$server2_name" \
    --arg url2 "$server2_url" \
'[$name1, $name2] as $names |
 [$url1, $url2] as $urls |
  [([$names, $urls] | transpose[]) as [$name, $url] |{$name, $url}]'

The benefit is that as the number of name/url pairs grows, you only need to modify the first two filters that define $names and $urls; the rest of the filter stays the same. You could even separate this into separate uses of jq, to facilitate the definition of a large list of servers.

names=$(jq -n --arg v1 "$server1_name" --arg v2 "$server2_name" '[$v1, $v2]')

urls=$(jq -n --arg v1 "$server1_url" --arg v2 "$server2_url" '[$v1, $v2]')

jq -n \
   --argjson names "$names" \
   --argjson urls "$urls" \
   '[([$names, $urls] | transpose[]) as [$name, $url] | {$name, $url}]'
🌐
Stack Overflow
stackoverflow.com › questions › 62973375 › how-to-create-an-array-from-json-object-using-jq
how to create an array from json object using jq - Stack Overflow
If you're dealing with just two properties from an object, just select the objects to put into an array, then put into an array. [ .element1, .element2 # select properties element1 then element2 ] # wrapped in [] creates the array
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.

🌐
Julius Gamanyi
til.juliusgamanyi.com › posts › jq-create-json-from-scratch
jq - Create json from scratch | Julius Gamanyi
June 1, 2023 - How do I create a json structure from scratch using only jq? The command line option --arg name value · passes a value to the jq program as a predefined variable… so --arg foo 123 will bind $foo to "123" - https://stedolan.github.io/jq/manual/ These named arguments are also available from ...
🌐
Atomic Spin
spin.atomicobject.com › jq-creating-updating-json
How to Use jq for Creating and Updating JSON Data
November 25, 2024 - Here are some examples of using the jq command-line utility to create new JSON data from scratch or update values in an existing JSON document.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-transform-json-data-with-jq
How To Transform JSON Data with jq | DigitalOcean
September 23, 2025 - You’ll have to refine your filter to get the names of all creatures and discard everything else. Since you’re working on an array, you’ll need to tell jq you want to operate on the values of that array instead of the array itself.
🌐
Eliatra
eliatra.com › home › blog › transform json data on bash using jq
Transform JSON Data on Bash Using jq
November 23, 2022 - Since we grouped by Department, it does not matter from which object we take it for each array index. We just grab it from the first. For getting the number of employees for each Department, we just apply the length function. This example also shows how we can create an entire new JSON structure as output, and reference values from the step before: ... $ cat staff.json | jq 'group_by (.Department)[] | {Department: .[0].Department, length: length}' { "Department": "IT", "length": 2 } { "Department": "Management", "length": 1 }
🌐
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 - Similar to the array construction there's also the object construction, with which objects can be created on the fly quite easily. And as the manual says: If the keys are "identifier-like", then the quotes can be left off · So I can use name rather than "name" for the property, reducing the JSON noise a little: jq -R '[.,inputs] | map(sub("^.+/";"")) | map({name: .})' names.txt
🌐
Alfred App Community
alfredforum.com › alfred workflows › workflow help & questions
Construct script filter JSON with jq from bash array - Workflow Help & Questions - Alfred App Community Forum
January 30, 2022 - Hi, I am creating a workflow and I want to create a script filter with strings from a bash array. Given I have an array called $regions that contains 3 strings. I want to create 3 list items from that using jq. echo $regions france netherlands denmark I tried this command but the output is obviou...