As the web seems to be moving to JavaScript Object Notation over XML for data transfer, it’s nice to find tools to help you work with JSON from the command line. Changelog listener Jason Williams pointed us to JSON Pipe, a simple Python command line utility for visualizing JSON document structure.

Installation and usage

Install JSON Pipe via pip

pip install jsonpipe

Now we can pipe data into jsonpipe to see a breakdown of the structure of the document:

$ echo '[{"a": [{"b": {"c": ["foo"]}}]}]' | jsonpipe
/   []
/0  {}
/0/a        []
/0/a/0      {}
/0/a/0/b    {}
/0/a/0/b/c  []
/0/a/0/b/c/0        "foo"

How about inspecting an NPM package?

curl https://github.com/jashkenas/coffee-script/raw/master/package.json | jsonpipe

/ {}
/name "coffee-script"
/description  "Unfancy JavaScript"
/keywords []
/keywords/0 "javascript"
/keywords/1 "language"
/keywords/2 "coffeescript"
/keywords/3 "compiler"
/author "Jeremy Ashkenas"
/version  "1.1.0-pre"
/licenses []
/licenses/0 {}
/licenses/0/type  "MIT"
/licenses/0/url "http://github.com/jashkenas/coffee-script/raw/master/LICENSE"
/engines  {}
/engines/node ">=0.2.5"
/directories  {}
/directories/lib  "./lib"
/main "./lib/coffee-script"
/bin  {}
/bin/coffee "./bin/coffee"
/bin/cake "./bin/cake"
/homepage "http://coffeescript.org"
/repository {}
/repository/type  "git"
/repository/url "git://github.com/jashkenas/coffee-script.git"

Since the output is regular STDOUT, JSON Pipe is piping hot:

curl https://github.com/jashkenas/coffee-script/raw/master/package.json | jsonpipe | grep "homepage"

/homepage   "http://coffeescript.org"

JSON Pipe will show you the key/value structure of your JSON documents faster than you can say XPath.

[Source on GitHub]


Have comments? Send a tweet to @TheChangelog on Twitter.

Subscribe to The Changelog Weekly – our weekly email covering everything that hits our open source radar.