Reputation: 21
Inside of a while loop I'm trying to build json objects and add them to an array then write this array to a json file using jq commands. Each json object in the array gets one of its properties from a file as shown below:
input="names.csv"
while IFS= read -r secretName || [ -n "$secretName" ];
do
value=""
value=$(echo "$secretValue" | tr -d '\n' | tr -d '\r' )
value="{\"name\":\"$secretName\",\"newValue\":\"****\"}" #get $secretname from names.txt
echo '[]' | jq ". += [$value]" > jsonOutput.json
done < $input
The expected output should be a JSON array of objects with multiple objects in it but I'm only seeing the last object. Since the input file has three names in it, the output should also have three objects in the array. I would also like to give the array a key and the output should look something like the following:
{"secrets":[
{
"name": "name1",
"newValue": "****"
},
{
"name": "name2",
"newValue": "****"
},
{
"name": "name3",
"newValue": "****"
}
]
}
I am very new to jq and the examples in various posts that I have seen left more questions than they answer. Please let me know why the first two objects are missing in the array and how I can assign a key to the array object.
Upvotes: 0
Views: 103
Reputation: 265717
Your loop looks unnecessarily complex (and maybe you don't even need it at all). Here's how you would construct a JSON array with computed values with jq in a shell loop:
input="names.csv"
while IFS= read -r secretName || [ -n "$secretName" ];
do
jq --arg name "$secretName" -n '{$name, newValue: "****"}'
done < "$input" | jq -s '{secrets:.}' > jsonOutput.json
This will create a stream of JSON objects first and will then collect them into a single array with the -s
(--slurp
) option.
It's also imperative, that you quote all your shell variables when expanding!
But you don't need to read the file with the shell at all, jq can already read text files just fine:
jq -R '{name: ., newValue: "****"}' "$input" | jq -s "{secrets:.}"
Alternatively, with a single jq process:
jq -Rn '[{name: inputs, newValue: "****"}]|{secrets:.}' "$input"
Upvotes: 0