Eric Radman : a Journal

Processing JSON on the Command Line

Parsing with Your Favorite Scripting Language

JSON has become the standard data interchange format of our time, and like XML it is not easily interpreted at the command line. For this reason I have often opened up a Ruby or Python interpreter and used the REPL to manually explore a structure in order to create a one-liner capable of parsing

>>> import json
>>> j = json.loads(open('github-tag.json').read())
>>> j.keys()
[u'forced', u'compare', u'pusher', u'sender', u'repository', u'created',
u'deleted', u'commits', u'after', u'head_commit', u'ref', u'base_ref',

If you want to browse around in your favorite editor instead you can get Python to pretty-print it

$ python -mjson.tool < github-tag.json > github-tag-pretty.json
$ vim github-tag-pretty.json

If you know the path of keys and indexes that you want to pull out of a JSON file your it your favorite scripting language has a way of extracting the data in one line

$ ruby22 -r json -e 'puts JSON.parse(["pusher"]' github-tag.json
{"email"=>"", "name"=>"eradman"}

sed for JSON data

Another tool, jq was created expressly for the purpose of slicing and transforming JSON structures. Like sed, learning jq comes with it's own domain-specific language which provides ennumerable ways of producing surprising results.

$h2 Flattened JSON

Last updated on November 18, 2015