title: Adding values to dynamic keys with jq
body: <p>I try to construct a json object with jq. I start with an empty object and want to add keys and values dynamically.</p>
  
  <p>This works but the key is not variable. It's fixed to "foo":</p>
  
  <pre><code>echo '{"foo": ["baz"]}' | jq --arg value "bar" '.foo += [$value]'
  </code></pre>
  
  <p>output as expected: </p>
  
  <pre><code>{"foo": ["baz", "bar"]}
  </code></pre>
  
  <p>What I actually want do do is something like this:</p>
  
  <pre><code>echo '{"foo": ["baz"]}' | jq --arg key "foo" --arg value "bar" '.($key) += [$value]'
  </code></pre>
  
  <p>Unfortunately this does not work. Here is the output:</p>
  
  <pre><code>jq: error: syntax error, unexpected '(' (Unix shell quoting issues?) at &lt;top-level&gt;, line 1:
  
  .($key) += [$value]
  
  jq: error: try .["field"] instead of .field for unusually named fields at &lt;top-level&gt;, line 1:
  
  .($key) += [$value]
  
  jq: 2 compile errors
  </code></pre>
  
  <p>I couldn't find a solution or figure it out.</p>
  
  <p>I know that this works: <code>jq --null-input --arg key foo '{($key): "bar"}'</code> but it doesn't solve my problem since I want to append values to existing lists as you can see in the examples. </p>