Beautifying JSON data from the shell

Yesterday I bumped into a very handly utility to beautify JSON data directly from your command line.

Let’s say that, with a command, you are retrieving some data in JSON format1:

1
2
3
4
curl -X POST --user admin:admin \
http://localhost:2480/command/temp/sql/select%20from%20person

{"result":[{"@type":"d","@rid":"#9:2","@version":0,"@class":"person","name":"Luca"}, {"@type":"d","@rid":"#9:3","@version":0,"@class":"person"}, {"@type":"d","@rid":"#9:4","@version":0,"@class":"person"}]}

Of course, the result doesn’t really look readable, but thanks to a python utility we can directly expand and beautify our JSON by piping:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
curl -X POST --user admin:admin \
http://localhost:2480/command/temp/sql/select%20from%20person \
| python -mjson.tool

{
    "result": [
        {
            "@class": "person",
            "@rid": "#9:2",
            "@type": "d",
            "@version": 0,
            "name": "Luca"
        },
        {
            "@class": "person",
            "@rid": "#9:3",
            "@type": "d",
            "@version": 0
        },
        {
            "@class": "person",
            "@rid": "#9:4",
            "@type": "d",
            "@version": 0
        }
    ]
}

That’s all, folks!

Notes
  1. In my case, I am retrieving data from OrientDB, a NoSQL graph=document DB which handles data in JSON format over its HTTP interface

In the mood for some more reading?

...or check the archives.