Pretty Print JSON With Python

JSON is a very common data format, but reading it can be a little difficult, especially if the JSON contains very little white space. If you have Python 2.6 or above you have use the json.tool to format the JSON so that you can read it correctly. This is also a good way to validate JSON strings that you have had to hand edit before they cause errors upstream.

If you have a file called file.json, which contains a bunch of JSON output, you can use Python to format this into a readable structure in the following way.

python -m json.tool file.json

You can also write this output directly into a file, in this case called formatted.json.

python -m json.tool file.json > formatted.json

The most common use of this tool is to pipe JSON to it from the command line (either directly or via a script).

echo '{"one":1,"two":[{"foo":1,"bar":2}]}' | python -m json.tool

This will produce the following output.

{
    "one": 1,
    "two": [
        {
            "bar": 2,
            "foo": 1
        }
    ]
}

You can also use this technique for testing scripts or curl requests, in fact anything that produces JSON output can be piped into json.tool and formatted correctly. Here is an example of piping a PHP script output into the json.tool.

php script.php | python -m json.tool

If you are testing a web service that produces JSON then you can test this using direct curl commands, which you can then pipe through json.tool to view the output.

curl -s http://localhost/service | python -m json.tool

What normally happens when you are debugging stuff is that you'll copy the JSON output into your clipboard and then create a file containing that output, which you can then format using the tool. A better way of doing this is to copy content directly from the clipboard and pipe that into the json.tool command. On OSX you would use pbpaste to do this.

pbpaste | python -m json.tool

If you are on Linux then you've probably got xclip, in which case you would use this.

xclip -selection clipboard -o | python -m json.tool

Lastly, I find it difficult to remember the syntax of the command needed here (not to mention the amount of characters) so I tend to create an alias to make it easier to understand . Add the following to your .bash_profile file.

alias asjson='python -m json.tool'

You can then do the same thing by piping the output through asjson, rather than the full command.

echo '{"one":1,"two":[{"foo":1,"bar":2}]}' | asjson

One thing you should be aware of is that this will sort the keys alphabetically, which might be a problem under certain circumstances.

Comments

In addition to this I've found out about a tool called jq (https://stedolan.github.io/jq/). Much easier to remember what command to pipe into than the python tool. You need to download and put it in the right place, but it's available for all platforms.
Name
Philip Norton
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.