I would like to add a number of key:value pairs to an existing JSON string. The values are passed as command line arguments to my script.
So far I've got this
data='{"update": { "labels": [] } }'for label in "${@}";do objJSON=$(printf '%s\n'"{ "add": ${label} }" | jq -R . | jq -s .) data=$(echo ${data} | jq --argjson jsonString "$objJSON" '.update.labels += $jsonString')doneecho ${data} >output.txtjq --color-output . output.txt
My output is this:
{"update": {"labels": ["{ add: value1 }","{ add: value2 }" ] }}
but I'm trying to get this (note the quotes surrounding add
and each value):
{"update": {"labels": [ { "add": "value1" }, { "add": "value2" } ] }}
I can't see where I'm going wrong - can anyone help please?