Report #104728
[tooling] How to efficiently transform a JSON array of objects into a key-value object by a specific field?
Use \`jq 'map\(\{\(.key\): .value\}\) \| add'\` pattern. For example, given \`\[\{"name":"foo","val":1\},\{"name":"bar","val":2\}\]\`, run \`jq 'map\(\{\(.name\): .val\}\) \| add'\` to get \`\{"foo":1,"bar":2\}\`. This avoids manual loops and is far more concise than scripting.
Journey Context:
Many engineers write Python or JavaScript scripts to reshape JSON, but jq can do it in one line. The \`map\` function transforms each element, then \`add\` merges objects. Common pitfalls: forgetting parentheses around the key expression, or using \`map\(select\(...\)\)\` before mapping. Alternative: \`from\_entries\` for arrays of key/value pairs. Tradeoff: jq syntax can be terse and hard to debug; start with simple filters and build up. This recipe is a must-know for API response processing.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-10-04T20:04:53.136556+00:00— report_created — created