<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged jq</title>
<link>https://coderlegion.com/tag/jq</link>
<description>Powered by Question2Answer</description>
<item>
<title>Three ways a jq one-liner lies to your bash script</title>
<link>https://coderlegion.com/28027/three-ways-a-jq-one-liner-lies-to-your-bash-script</link>
<description>&lt;p&gt;Most &quot;the filter looks right but the script misbehaves&quot; bugs come down to three silent failure modes.&lt;/p&gt;
&lt;p&gt;First, a missing &lt;code&gt;-r&lt;/code&gt;. jq outputs JSON by default, so a string arrives with its quotes attached — your variable holds &lt;code&gt;&quot;web-01&quot;&lt;/code&gt;, quotes included, and a comparison against web-01 never matches. Any value headed into a shell variable, filename, or test wants &lt;code&gt;-r&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Second, missing keys don't error. jq emits &lt;code&gt;null&lt;/code&gt;, which lands in bash as a literal four-character string — non-empty, so &lt;code&gt;[ -n ]&lt;/code&gt; guards pass and backup-null.tar.gz gets written. The &lt;code&gt;//&lt;/code&gt; operator supplies a real fallback.&lt;/p&gt;
&lt;p&gt;Third, select() compares exactly. A value that is almost right matches nothing, and nothing is not an error:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;jq -r '.items[] | select(.active == true) | .name'
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the API sends &lt;code&gt;&quot;active&quot;: &quot;true&quot;&lt;/code&gt; as a string, that item is skipped. If you match &lt;code&gt;&quot;Web-01&quot;&lt;/code&gt; against &lt;code&gt;&quot;web-01&quot;&lt;/code&gt;, zero elements come back. Either way jq prints nothing and exits 0, so the silence reads as success.&lt;/p&gt;
&lt;p&gt;All three share the same fingerprint: exit code zero, plausible output, wrong result. The fastest way I've found to catch them before they ship is building the filter against the actual response: this &lt;a rel=&quot;nofollow&quot; href=&quot;https://bashsnippets.xyz/tools/jq-filter-builder&quot;&gt;interactive jq filter builder with a live in-browser preview&lt;/a&gt; lets you click through your real JSON to build the path, handles the &lt;code&gt;-r&lt;/code&gt;, the &lt;code&gt;//&lt;/code&gt; fallback and select() quoting for you, and shows exactly what the filter emits before anything runs.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/28027/three-ways-a-jq-one-liner-lies-to-your-bash-script</guid>
<pubDate>Tue, 27 Oct 2026 13:00:00 +0000</pubDate>
</item>
</channel>
</rss>