title: How to check if the json file has a specific element?
body: <p>How to check if ["foo"]["bar"] is present in a json file from BASH script? tried below:</p>
  
  <pre><code>$ cat myJson.json | if ( python3 -c "import sys,json; json.load(sys.stdin)['foo']['bar']"); then echo "found foo-bar"; fi
  $ found foo-bar
  </code></pre>
  
  <p>it works, but it prints below if the element is not present:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;string&gt;", line 1, in &lt;module&gt;
  KeyError: 'baz'
  </code></pre>
  
  <p>but bash script don't fail anyway.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Tried try-except:</p>
  
  <pre><code>cat myJson.json | \
  if (
  python3 -c "
  import sys,json
  try:
     json.load(sys.stdin)['foo']['bar']
  except KeyError:
     #exit with non-0.
     sys.exit(1)
  ") then echo "found foo_bar"; FOUND_FOO_BAR=true;
  fi
  echo $FOUND_FOO_BAR
  $ found foo_bar
  $ 
  $
  </code></pre>
  
  <p>I am expecting "true" to be printed after `found foo_bar', but it doesn't, what am I doing wrong?</p>