as described in "Add new element to existing JSON array with jq" I am using this jq form to add elements to a JSON array.
This works fine in the command line. Short minimal version here:
jq '."schemes" += [{ JSON STRING HERE }]' settings.json > settings.json.newLong, complete version here, I am trying to add a colorscheme to the windows terminal app:
jq '."schemes" += [{"name": "Grape","black": "#2d283f","red": "#ed2261","green": "#1fa91b","yellow": "#8ddc20","blue": "#487df4","purple": "#8d35c9","cyan": "#3bdeed","white": "#9e9ea0","brightBlack": "#59516a","brightRed": "#f0729a","brightGreen": "#53aa5e","brightYellow": "#b2dc87","brightBlue": "#a9bcec","brightPurple": "#ad81c2","brightCyan": "#9de3eb","brightWhite": "#a288f7","background": "#171423","foreground": "#9f9fa1","selectionBackground": "#493d70","cursorColor": "#a288f7"}]' ./settings.json > ./settings.json.newAs said, this works great, and the new colorscheme shows up in Windows Terminal settings. However, when trying to automate this in a shell (bash) script, jq chokes.
# usage is add_colorscheme.sh colorscheme.jsoncolorscheme=$1json_content=`cat ${colorscheme}`json_file="settings.json"jq '."schemes" += [${json_content}] ${json_file} > ${json_file}.newI have tried many ways to expand variables, e.g. without braces ($json_content), and have also tried without success with other incantations. For example, using --argjson in the terminal:
json_content=`cat file.json`echo ${json_content}{ "name": "Grape", "black": "#2d283f", "red": "#ed2261", "green": "#1fa91b", "yellow": "#8ddc20", "blue": "#487df4", "purple": "#8d35c9", "cyan": "#3bdeed", "white": "#9e9ea0", "brightBlack": "#59516a", "brightRed": "#f0729a", "brightGreen": "#53aa5e", "brightYellow": "#b2dc87", "brightBlue": "#a9bcec", "brightPurple": "#ad81c2", "brightCyan": "#9de3eb", "brightWhite": "#a288f7", "background": "#171423", "foreground": "#9f9fa1", "selectionBackground": "#493d70", "cursorColor": "#a288f7" }# but jq failsjq --argjson content ${json_content} '."schemes" += [$content]'jq: invalid JSON text passed to --argjsonUse jq --help for help with command-line options,or see the jq manpage, or online docs at https://jqlang.github.io/jqor --arg
jq -n --arg content ${json_content} '."schemes" += [env.content]' settings.jsonjq: error: syntax error, unexpected ':', expecting end of file (Unix shell quoting issues?) at <top-level>, line 1:"name":jq: 1 compile errorI have read a number of other posts like this one, on the topic of jq and variable expansions, but not sure how to even try here strings when I can't even get --arg nor --argjson to work.
I have tried exchanging -n --argjson to --argjson foo bar -n to no avail. I have even read the manpage! There's nothing special about --argjson there that I can see.
What am I doing wrong? Thanks for any help!
My system and context:Ubuntu 24.04.2 LTS, WSL2jq: v1.7
colorscheme.json:
{"name": "Grape","black": "#2d283f","red": "#ed2261","green": "#1fa91b","yellow": "#8ddc20","blue": "#487df4","purple": "#8d35c9","cyan": "#3bdeed","white": "#9e9ea0","brightBlack": "#59516a","brightRed": "#f0729a","brightGreen": "#53aa5e","brightYellow": "#b2dc87","brightBlue": "#a9bcec","brightPurple": "#ad81c2","brightCyan": "#9de3eb","brightWhite": "#a288f7","background": "#171423","foreground": "#9f9fa1","selectionBackground": "#493d70","cursorColor": "#a288f7"}settings.json:
{"$help": "https://aka.ms/terminal-documentation","$schema": "https://aka.ms/terminal-profiles-schema","language": "en-US","themes": [],"schemes": []}