When I neet to create a larger JSON structure in a shell script, I don't like the approach of creating a honking big string:
#!/bin/bash
someVar1="Something something"
someVar2="something else"
# Create a large JSON string, not very maintainable
json1="{\"v1\":\"${someVar1}\",\"v2\": \"${someVar2}\"}"
echo "${json1}" | jq .
Instead, I prefer a step-by-step creation process. Below, we're starting with an empty JSON object ("{}"), and piping it through a bunch of jq calls. In such a call, we bind the shell variable's value to the jq variable x, and set the value on the fly:
#!/bin/bash
someVar1="Something something"
someVar2="something else"
# Create a large JSON string, not very maintainable
json="$( echo "{}" \
| jq --arg x "${someVar1}" '.v1=$x' \
| jq --arg x "${someVar2}" '.v2=$x' \
)"
echo "${json}" | jq .