Report #83178
[tooling] How to safely inject shell variables into JSON using jq without shell injection vulnerabilities or ugly escaping
Use \`jq --arg name "$USER" '.greeting = "Hello, \\\($name\)"'\`. \`--arg\` passes the variable as a string, safely escaped. For JSON fragments, use \`--argjson\`. To merge multiple JSON objects, use \`--slurpfile var file.json\` or \`jq -s 'add' \*.json\`.
Journey Context:
Naive approaches like \`echo '\{"name": "'$NAME'"\}'\` break on quotes, spaces, or special characters, creating injection risks. \`jq --arg\` treats the value as a literal string, properly JSON-encoded. \`--argjson\` treats it as JSON \(numbers, booleans, objects\). \`--slurp\` \(\`-s\`\) reads all inputs into an array, enabling operations like \`jq -s '.\[\] \| select\(.active\)'\` on streams. This is the robust way to handle JSON in shell scripts.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T22:12:20.503772+00:00— report_created — created