Search notes:

jq --arg

jq --arg nam val assigns the value val to the variable nam. The variable can then be referred to in the jq program as $nam.
The arguments passed with --arg are interpreted as strings:
$ jq  --arg num1 40 --arg num2 2 -n '$num1 + $num2'
"402"
In order to convert them to a number the tonumber operator can be used:
$ jq --arg num1 40 --arg num2 2 -n '($num1 | tonumber) + ($num2 | tonumber)'

Create a JSON document from a template

template='{
   "id" :  $id,
   "val":  $val
}'

payload=$(jq -n --arg id 42 --arg val 'Hello world' "$template")

echo $payload
$payload is { "id": "42", "val": "Hello world" }.
Note that 42 and Hello world are interpreted as strings.
Use --argjson to produce a numerical id and a string val:
payload=$(jq -n --argjson id 42 --argjson val '"Hello world"' "$template")
echo $payload
With --argjson, payload is { "id": 42, "val": "Hello world" }.

See also

--arg often seems to be combined with -n.

Index