title: Add key value to parent subelement if child has specific key:value
body: <p>I'm trying to understand what's the best way to add a json element to child's parent
  if that child contains a specific key:value and finally print the entire json using jq
  I try to explain better with an example.
  The input json is:</p>
  <pre><code>{
    &quot;family&quot;: {
      &quot;surname&quot;: &quot;Smith&quot;
    },
    &quot;components&quot;: [
      {
        &quot;name&quot;: &quot;John&quot;,
        &quot;details&quot;: {
          &quot;hair&quot;: &quot;brown&quot;,
          &quot;eyes&quot;: &quot;brown&quot;,
          &quot;age&quot;: &quot;56&quot;
        },
        &quot;role&quot;: &quot;father&quot;
      },
      {
        &quot;name&quot;: &quot;Mary&quot;,
        &quot;details&quot;: {
          &quot;hair&quot;: &quot;blonde&quot;,
          &quot;eyes&quot;: &quot;green&quot;,
          &quot;age&quot;: &quot;45&quot;
        },
        &quot;role&quot;: &quot;mother&quot;
      },
      {
        &quot;name&quot;: &quot;George&quot;,
        &quot;details&quot;: {
          &quot;hair&quot;: &quot;blonde&quot;,
          &quot;eyes&quot;: &quot;brown&quot;,
          &quot;age&quot;: &quot;25&quot;
        },
        &quot;role&quot;: &quot;child&quot;
      }
    ]
  }
  </code></pre>
  <p>I want to add:
  &quot;description&quot;: &quot;5 years less than 30&quot;
  at the same level of &quot;details&quot; if &quot;age&quot; is equal to &quot;25&quot; and then print the result:</p>
  <pre><code>{
    &quot;family&quot;: {
      &quot;surname&quot;: &quot;Smith&quot;
    },
    &quot;components&quot;: [
      {
        &quot;name&quot;: &quot;John&quot;,
        &quot;details&quot;: {
          &quot;hair&quot;: &quot;brown&quot;,
          &quot;eyes&quot;: &quot;brown&quot;,
          &quot;age&quot;: &quot;56&quot;
        },
        &quot;role&quot;: &quot;father&quot;
      },
      {
        &quot;name&quot;: &quot;Mary&quot;,
        &quot;details&quot;: {
          &quot;hair&quot;: &quot;blonde&quot;,
          &quot;eyes&quot;: &quot;green&quot;,
          &quot;age&quot;: &quot;45&quot;
        },
        &quot;role&quot;: &quot;mother&quot;
      },
      {
        &quot;name&quot;: &quot;George&quot;,
        &quot;details&quot;: {
          &quot;hair&quot;: &quot;blonde&quot;,
          &quot;eyes&quot;: &quot;brown&quot;,
          &quot;age&quot;: &quot;25&quot;
        },
        &quot;role&quot;: &quot;child&quot;,
        &quot;description&quot;: &quot;5 years less than 30&quot;
      }
    ]
  }
  </code></pre>
  <p>The only solution I've found was to apply the update but printing only the &quot;components&quot; content;
  then I've removed from the JSON and finally inserted the modified &quot;components&quot; content previously saved, in this way:</p>
  <pre><code>cat sample.json | jq -c ' .components[] | select(.details.age==&quot;25&quot;) |= . + {description: &quot;5 years less than 30&quot; } ' &gt; /tmp/saved-components.tmp
  cat sample.json | jq --slurpfile savedcomponents /tmp/saved-components.tmp 'del(.components) | . + { components: [ $savedcomponents ] }'
  </code></pre>
  <p>I don't think it's the best way to solve these kind of problems, so I'd like to know what is
  the right &quot;jq approach&quot;.
  I forgot to say: I prefer to use jq only, not other tools</p>
  <p>Than you</p>
  <p>Marco</p>