title: jq: map object if field exists
body: <p>This is my jq code:</p>
  <pre><code>def pick_nationality:
    {nation: {country: .NACIONALITAT, code: &quot;some code&quot;} };
  
  def pick_surname:
    {name: {surname: .SURNAME, code: &quot;some code&quot;} };
  
  map([pick_nationality, pick_surname])
  </code></pre>
  <p>Problem here appears when some of <code>.NACIONALITAT</code> or <code>.SURNAME</code> is not present on input objects:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;SURNAME&quot;: &quot;surname1&quot;
  }
  {
    &quot;NACIONALITAT&quot;: &quot;nacionalitat1&quot;
  }
  </code></pre>
  <p>Result:</p>
  <pre class="lang-json prettyprint-override"><code>[
    [
      {
        &quot;nation&quot;: {
          &quot;country&quot;: null,
          &quot;code&quot;: &quot;some code&quot;
        }
      },
      {
        &quot;name&quot;: {
          &quot;surname&quot;: &quot;surname1&quot;,
          &quot;code&quot;: &quot;some code&quot;
        }
      }
    ],
    [
      {
        &quot;nation&quot;: {
          &quot;country&quot;: &quot;nacionalitat1&quot;,
          &quot;code&quot;: &quot;some code&quot;
        }
      },
      {
        &quot;name&quot;: {
          &quot;surname&quot;: null,
          &quot;code&quot;: &quot;some code&quot;
        }
      }
    ]
  ]
  
  </code></pre>
  <p>Problem is I need to avoid pick_natinality when <code>.NATIONALITAT</code> field is not present...</p>
  <p>Desired result would be:</p>
  <pre class="lang-json prettyprint-override"><code>[
    [
      {
        &quot;name&quot;: {
          &quot;surname&quot;: &quot;surname1&quot;,
          &quot;code&quot;: &quot;some code&quot;
        }
      }
    ],
    [
      {
        &quot;nation&quot;: {
          &quot;country&quot;: &quot;nacionalitat1&quot;,
          &quot;code&quot;: &quot;some code&quot;
        }
      }
    ]
  ]
  
  </code></pre>
  <p>Any ideas?</p>