Report #97644
[tooling] How to construct a JSON object with dynamic keys from shell variables using jq?
Use \`jq -n --arg key "value" '\{\($key\): "somevalue"\}'\` to build objects where the key is taken from a variable, avoiding unsafe shell interpolation.
Journey Context:
Naively using \`jq "\{\\"$var\\": ...\}"\` is error-prone and insecure. The \`--arg\` flag passes values safely, and the \`\($key\)\` syntax inside the object constructor uses the variable as a key. This pattern is essential for scripting JSON generation in CI/CD pipelines. Alternatives like \`printf\` or \`sed\` are fragile. This approach is documented in jq's manual under advanced features.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-25T15:47:21.369530+00:00— report_created — created