title: What are good CLI tools for JSON?
body: <h2>General Problem</h2>
  
  <p>Though I may be diagnosing the root cause of an event, determining how many users it affected, or distilling timing logs in order to assess the performance and throughput impact of a recent code change, my tools stay the same: <code>grep</code>, <code>awk</code>, <code>sed</code>, <code>tr</code>, <code>uniq</code>, <code>sort</code>, <code>zcat</code>, <code>tail</code>, <code>head</code>, <code>join</code>, and <code>split</code>. To glue them all together, Unix gives us pipes, and for fancier filtering we have <code>xargs</code>. If these fail me, there's always <code>perl -e</code>.</p>
  
  <p>These tools are perfect for processing CSV files, tab-delimited files, log files with a predictable line format, or files with comma-separated key-value pairs. In other words, files where each line has next to no context.</p>
  
  <h2>XML Analogues</h2>
  
  <p>I recently needed to trawl through Gigabytes of XML to build a histogram of usage by user. This was easy enough with the tools I had, but for more complicated queries the normal approaches break down. Say I have files with items like this:</p>
  
  <pre><code>&lt;foo user="me"&gt;
      &lt;baz key="zoidberg" value="squid" /&gt;
      &lt;baz key="leela"    value="cyclops" /&gt;
      &lt;baz key="fry"      value="rube" /&gt;
  &lt;/foo&gt;
  </code></pre>
  
  <p>And let's say I want to produce a mapping from user to average number of <code>&lt;baz&gt;</code>s per <code>&lt;foo&gt;</code>. Processing line-by-line is no longer an option: I need to know which user's <code>&lt;foo&gt;</code> I'm currently inspecting so I know whose average to update. Any sort of Unix one liner that accomplishes this task is likely to be inscrutable.</p>
  
  <p>Fortunately in XML-land, we have wonderful technologies like XPath, XQuery, and XSLT to help us.</p>
  
  <p>Previously, I had gotten accustomed to using the wonderful <code>XML::XPath</code> Perl module to accomplish queries like the one above, but after finding a <a href="http://ditchnet.org/xmlmate/" rel="noreferrer">TextMate Plugin that could run an XPath expression against my current window</a>, I stopped writing one-off Perl scripts to query XML. And I just found out about <a href="http://xmlstar.sourceforge.net/" rel="noreferrer">XMLStarlet</a> which is installing as I type this and which I look forward to using in the future.</p>
  
  <h2>JSON Solutions?</h2>
  
  <p>So this leads me to my question: are there any tools like this for JSON? It's only a matter of time before some investigation task requires me to do similar queries on JSON files, and without tools like XPath and XSLT, such a task will be a lot harder. If I had a bunch of JSON that looked like this:</p>
  
  <pre><code>{
    "firstName": "Bender",
    "lastName": "Robot",
    "age": 200,
    "address": {
      "streetAddress": "123",
      "city": "New York",
      "state": "NY",
      "postalCode": "1729"
    },
    "phoneNumber": [
      { "type": "home", "number": "666 555-1234" },
      { "type": "fax", "number": "666 555-4567" }
    ]
  }
  </code></pre>
  
  <p>And wanted to find the average number of phone numbers each person had, I could do something like this with XPath:</p>
  
  <pre><code>fn:avg(/fn:count(phoneNumber))
  </code></pre>
  
  <h2>Questions</h2>
  
  <ol>
  <li>Are there any command-line tools
  that can "query" JSON files in this
  way?</li>
  <li>If you have to process a bunch of
  JSON files on a Unix command line,
  what tools do you use?</li>
  <li>Heck, is there even work being done
  to make a query language like this
  for JSON?</li>
  <li>If you do use tools like this in
  your day-to-day work, what do you
  like/dislike about them? Are there
  any gotchas?</li>
  </ol>
  
  <p>I'm noticing more and more data serialization is being done using JSON, so processing tools like this will be crucial when analyzing large data dumps in the future. Language libraries for JSON are very strong and it's easy enough to write scripts to do this sort of processing, but to really let people play around with the data shell tools are needed.</p>
  
  <h3>Related Questions</h3>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/91791/grep-and-sed-equivalent-for-xml-command-line-processing">Grep and Sed Equivalent for XML Command Line Processing</a></li>
  <li><a href="https://stackoverflow.com/questions/777455/is-there-a-query-language-for-json">Is there a query language for JSON?</a></li>
  <li><a href="https://stackoverflow.com/questions/859033/jsonpath-or-other-xpath-like-utility-for-json-javascript-or-jquery-json">JSONPath or other XPath like utility for JSON/Javascript; or Jquery JSON</a></li>
  </ul>