I'm creating JSON objects from given data. If an JSON object is created it should be appended to an JSON array which is stored in a bash-variable. This batch of JSON objects should later be send via curl.
Code is:
declare -a data=('10.4' '100.23' '20.02');
batch="[]"
for data_object in "${data[@]}"; do
json=$(jq -n --arg inf $data_object '{data: $inf|tonumber}')
batch=$(jq '$batch += ["$json"]')
done
The expected result after n loops should be:
[
{
"data": 10.4
},
{
"data": 100.23
},
{
"data": 20.02
}
]
I'm constantly getting this error: jq: error: $batch is not defined at <top-level>, line 1:. How can I solve this?
batchis used in shell context, it is not automatically imported to jq'sbatch="[$(printf '{"information": %s}\n' "${data[@]}" | paste -sd,)]"?jq. Post a minimal reproducible example with an input and an exact output needed$informationcoming from?