I have a loop where I'm creating a json structure using jq. I create individual json structures with the following.
jq -n \
--arg name $myname \
--arg subject $mysubject \
--arg version "1" \
'{name: $name, subject: $subject, version: $version}'
But I want to concatenate each structure in to an array, like:
[
{
"name": "name1",
"subject": "subj1",
"version": "1"
},
{
"name": "name2",
"subject": "subj2",
"version": "1"
}
]
I'm trying something like this with pipes, but the array doesn't get populated.
$myarray=$myarray jq -n \
--arg name $myname \
--arg subject $mysubject \
--arg version "1" \
'{name: $name, subject: $subject, version: $version}'
--arg name "$myname",--arg subject "$mysubject". By contrast, you don't need quotes around the1in--arg version 1(they don't hurt anything, but they don't help you at all either). Otherwise names or subjects with whitespace will misbehave.myarray. Just like when you store any other command's output into a variable, you need to use a command substitution; we have lots of non-jq-specific duplicates on that.var=$(yourcommand); the$(...)is critical.smtpHost=$(jq -r '.smtp.host' settings.json)