Pretty Printing JSON on the Command Line with Python

You may often find yourself on a command-line, for instance when SSH'd into a server, and need to read some JSON. This could be a JSON configuration file, or indeed it could be simply a response from an API endpoint.

Either way, the JSON you're getting back isn't quite as nicely formatted as you'd hoped. You think about manually going through and editing it to clean it up, but want to know if there's a better way.

On modern GNU/Linux systems, there will usually be a minimal install of Python, as a number of system tools have a dependency on it. Within this minimal install, there should be the json python.

This library has a module (a file within a library, in Python speak) called tool, which has the ability to take a JSON file (or input from stdin) and output the corresponding pretty-printed JSON.

Given the following minified JSON file that we want to be able to inspect:


Let's use the following pipeline to output it:

$ cat file.json | python -m json.tool
#                        ^ invoke a specific library from Python
#                           ^ within the JSON library
#                                ^ call the tool module
# ^ useless use of cat, use recommended pipeline below instead:
$ python -m json.tool < file.json
    "key": [
    "key2": "value"

To see this article in action, check out the asciicast:

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #python #json #pretty-print.

This post was filed under articles.

This post is part of the series pretty-print-json.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.