I'm working with a json document where the property keys are filenames, and the values are the intended content of those files, where the files are mostly Java properties files.
I'm writing a Bash script that produces a modified json, where the change is the text in a single "line" of the content of one of the json properties. This is essentially changing the value of a single property in the embedded Java properties file.
If it matters, the json document is a kubernetes configmap, but that doesn't matter for the problem I'm having.
The process is essentially this:
augfile=$(cat $outfile | sed -e "s/${property}[ ]*=.*$/${property}=${newValue}/")
kubectl get configmap $configmap -o json | jq ".data[\"$filename\"] = \"$augfile\"" | kubectl replace $configmap -f -
What I see in the output is this:
+ kubectlidp dev2 get configmap onemapms-green-configs -o json
+ jq '.data["application.properties"] = "...
...
listprop =\
abc=def,\
ghi=jkl,\
mno=pqr
..."'
+ kubectl replace <configmapname> -f -
jq: error: Invalid escape at line 2, column 1 (while parsing '"\
"') at <top-level>, line 129:
listprop =\
It's clear that I have to modify the way I specify the "$augfile" value, but I'm lost in the quotes there.
Although you can sometimes get away with using shell string interpolation to "construct" the jq program, it's almost always better to pass in the shell values using the jq command-line options --arg and/or --argjson, e.g. in your case (assuming your shell allows this style of quoting):
jq --arg filename "$filename" --arg augfile "$augfile" '
.data[$filename] = $augfile'