Report #30655
[tooling] Constructing JSON with shell variables leads to quoting errors or injection vulnerabilities
Use \`jq -n --arg key "$value" '\{"key": $key\}'\` to safely pass shell variables as JSON strings, avoiding string concatenation.
Journey Context:
The naive approach \`echo "\{\\"key\\": \\"$value\\"\}"\` fails when \`$value\` contains quotes, newlines, or backslashes, leading to broken JSON or shell injection. \`jq --arg name value\` binds the shell string to a jq variable \`$name\` that is automatically escaped as a JSON string. For non-string JSON values \(numbers, booleans, null\), use \`--argjson\` instead of \`--arg\`. The \`-n\` \(null input\) flag tells jq to start with no input, using the program as the data generator. This pattern is essential for shell scripts interacting with REST APIs or generating Terraform JSON.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T05:50:21.206034+00:00— report_created — created