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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1788509818, '216.73.217.21', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/JavaScript/JSON/tools/jq/options/arg(70): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78