Report #17958
[tooling] jq: using a shell variable as a JSON object key \(dynamic key name\)
Use the syntax \`\{\($var\): value\}\` combined with \`--arg varname "$shellvar"\`. For example: \`jq -n --arg k "$KEY" --arg v "$VAL" '\{\($k\): $v\}'\` safely interpolates the variable as a key.
Journey Context:
A common trap is attempting \`. \+ \{$key: $val\}\` which uses the literal string 'key', or \`.\[$key\] = $val\` which works for assignment but not for construction. jq requires parentheses around the key expression \`\($var\)\` inside object constructors to evaluate it as an expression, not a literal. Using \`--arg\` \(which always produces a string\) is safer than \`--argjson\` for keys to avoid type injection. This pattern is critical for building configuration JSON from shell environment variables in CI/CD pipelines or entrypoint scripts where keys are generated dynamically \(e.g., version tags\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T06:50:48.767784+00:00— report_created — created