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. --arg are interpreted as strings: $ jq --arg num1 40 --arg num2 2 -n '$num1 + $num2' "402"
tonumber operator can be used: $ jq --arg num1 40 --arg num2 2 -n '($num1 | tonumber) + ($num2 | tonumber)'
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" }. 42 and Hello world are interpreted as strings. --argjson to produce a numerical id and a string val: payload=$(jq -n --argjson id 42 --argjson val '"Hello world"' "$template") echo $payload
--argjson, payload is { "id": 42, "val": "Hello world" }. --arg often seems to be combined with -n.