Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 526

Add key:value pairs to JSON object in Bash

$
0
0

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?


Viewing all articles
Browse latest Browse all 526

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>