<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>CLI on Vedant Andhale</title>
    <link>https://www.vedant.me/tags/cli/</link>
    <description>Recent content in CLI on Vedant Andhale</description>
    <image>
      <url>https://www.vedant.me/</url>
      <link>https://www.vedant.me/</link>
    </image>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 10 Sep 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://www.vedant.me/tags/cli/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>jq keys_unsorted is not an ordering contract</title>
      <link>https://www.vedant.me/notebook/jq-keys_unsorted-preserves-order/</link>
      <pubDate>Thu, 10 Sep 2026 00:00:00 +0000</pubDate>
      
      <guid>https://www.vedant.me/notebook/jq-keys_unsorted-preserves-order/</guid>
      <description>Use keys_unsorted for convenient inspection; use an array when the order is part of the data.</description>
      <content:encoded><![CDATA[<p><code>jq</code> has two convenient ways to inspect an object&rsquo;s keys:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="nb">printf</span> <span class="s1">&#39;%s\n&#39;</span> <span class="s1">&#39;{&#34;b&#34;:2,&#34;a&#34;:1}&#39;</span> <span class="p">|</span> jq <span class="s1">&#39;keys&#39;</span>
</span></span><span class="line"><span class="cl"><span class="c1"># [&#34;a&#34;, &#34;b&#34;]</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">printf</span> <span class="s1">&#39;%s\n&#39;</span> <span class="s1">&#39;{&#34;b&#34;:2,&#34;a&#34;:1}&#39;</span> <span class="p">|</span> jq <span class="s1">&#39;keys_unsorted&#39;</span>
</span></span><span class="line"><span class="cl"><span class="c1"># Typically [&#34;b&#34;, &#34;a&#34;]</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p><code>keys</code> sorts the result. The jq manual describes <code>keys_unsorted</code> as returning keys roughly in insertion order. That makes it useful when inspecting a document, but it is weaker than a guarantee that every transformation will preserve an intended order.</p>
<p>If order matters to the application, put it in an array:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl"><span class="p">[</span>
</span></span><span class="line"><span class="cl">  <span class="p">{</span><span class="nt">&#34;name&#34;</span><span class="p">:</span> <span class="s2">&#34;b&#34;</span><span class="p">,</span> <span class="nt">&#34;value&#34;</span><span class="p">:</span> <span class="mi">2</span><span class="p">},</span>
</span></span><span class="line"><span class="cl">  <span class="p">{</span><span class="nt">&#34;name&#34;</span><span class="p">:</span> <span class="s2">&#34;a&#34;</span><span class="p">,</span> <span class="nt">&#34;value&#34;</span><span class="p">:</span> <span class="mi">1</span><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">]</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>Now <code>jq '.[].name'</code> follows an explicit sequence. A consumer does not have to infer a display order from the way an object happened to be written or reconstructed.</p>
<p>I would use <code>keys_unsorted</code> for a quick look at familiar input. For a report, API contract or reproducible export, I would define the ordering directly or sort by an explicit field.</p>
<p>Reference: <a href="https://jqlang.org/manual/">jq manual: keys and keys_unsorted</a>.</p>
]]></content:encoded>
    </item>
    
  </channel>
</rss>
