title: How to use jq to selectively update JSON array, referring to parent properties
body: <p>I'm trying to use <a href="https://stedolan.github.io/jq/" rel="nofollow noreferrer">jq</a> to selectively change a single property of an object nested in an array, keeping the rest of the array intact. The generated property value needs to refer up to a property of the parent object.</p>
  
  <p>As an example, take this array:</p>
  
  <pre class="lang-json prettyprint-override"><code>[
    {
      "name": "keep_me"
    },
    {
      "name": "leave_unchanged",
      "list": [
        {
          "id": "1",
          "key": "keep_this"
        }
      ]
    },
    {
      "name": "a",
      "list": [
        {
          "id": "2",
          "key": "also_keep_this"
        },
        {
          "id": "42",
          "key": "replace_this"
        }
      ]
    }
  ]
  </code></pre>
  
  <p>I want to change the value of the last key (<code>replace_this</code>), using the <code>name</code> property of the parent object to generate a value like <code>generated_value_for_a_42</code>.</p>
  
  <p>The key problem here seems to be leaving the rest of the array unmodified, while updating specific elements. But the need to refer 'up the tree' to a parent property complicates matters.
  I tried wrapping changes in parentheses to keep untouched elements, but then had trouble with variable binding (using <code>as</code>) to the right scope, for accessing the parent property. So I either ended up discarding parts of the array or objects, or getting errors about the variable binding.</p>