title: How to validate a query in jq 1.5 in macOS/Linux bash?
body: <p>I am trying my darnedest to figure out how to tell a user of one of my scripts that they passed a query that is invalid per <a href="https://stedolan.github.io/jq/manual/v1.5/" rel="nofollow noreferrer"><code>jq 1.5</code></a>.</p>
  
  <p>Here is a simple example JSON:</p>
  
  <pre><code>{ "foo": { "bar":"baz" } } 
  </code></pre>
  
  <p>I want to be able to present an error message to my user when they provide a query like <code>.foo.bax</code> but have it return the value <code>baz</code> when they provide a correct query like <code>.foo.bar</code>.</p>
  
  <p>I cannot <em>(or do not know how to)</em> read <code>jq</code>'s return values because I need to run <code>jq</code> in a subshell, i.e.:</p>
  
  <pre><code>value="$(echo -e "${json}" | jq -r "${query}")"
  </code></pre>
  
  <p>The <code>jq</code> always return a <code>null</code> string when I give it an invalid query.  But I cannot differentiate that from a value of <code>null</code> that might be contained in a JSON file as <code>"property": "null"</code>.</p>
  
  <p>It appears <code>jq</code> has various functions that I might be able to use for this, but I cannot figure out how I would use them.</p>
  
  <p>So is there any way to get <code>jq</code> to validate a query where I can tell the difference between a <code>null</code> and a <code>"null"</code>? </p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>What I was hoping to find when I asked the question is a syntax that would be roughly equivalent to <code>"$(jq.validate("${query}") &amp;&amp; echo "valid")"</code>, assuming <code>jq</code> had a <code>validate()</code> method.</p>