I would like to add bash variables to a some JSON file with jq. The following method works:
jq --arg VAR "$VAR" '.+{VAR:$VAR}' my.json > my.tmp.json && mv my.tmp.json my.json
with VAR being a bash variable. But as I'm adding many variables I would like to extract a function like the following:
#$1 Variable Name; The entry {"$1": "$($1)"} should be added to my.json
config_add() {
COMMAND="jq --arg VAR \"\$VAR\" '.+{VAR:\$VAR}' my.json"
#Replace VAR with the argument
${COMMAND//VAR/$1} my.json > my.json.tmp && my.json.tmp my.json
}
But for any variant I try I receive:
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:'.+{MYVAR:$MYVAR}'
Any Ideas what is happening here? How would one quote it correctly? Thank you!