How to Pretty Print a JSON File in Python

A set of makeup brushes.

Like many other programming languages, Python works well with JavaScript Object Notation (JSON) data. It can pull this data in from a few sources and work with it like any other file. This is helpful when you want to pretty print a JSON file in Python.

In this post, we show you how to pretty print a JSON file in two ways. Both will involve using the Terminal and command line, although you may not need Python knowledge at all for one of them.

What Pretty Print Is (And Why You’d Want to Use It With JSON Files)

In short, pretty printing takes data in a raw format and alters it to something readable. In other words, it makes it pretty.

Prettified data in Neovim.

Under usual circumstances, you’ll do this with files pulled into your program that need to be parsed by a human. Often, data from other file formats (such as JSON files) will have been “minified.” This strips away all of the white space from the code in order to make it leaner for a computer to parse.

For example, you could take raw database data that’s been minified and run it through a script to add the correct indentation and other formatting. From there, you can export it to others and work with it as you wish.

How to Pretty Print a JSON File in Python

Below, we show you how to pretty print a JSON file in Python. For our examples, we use NeoVim and the built-in :term command. However, there’s no reason you couldn’t do this direct from the Terminal itself in the Python interpreter.

1. Define Your JSON File

First, we want to have some JSON data to work with. There are two approaches you can take – by defining data within your Python script or through a JSON file. We’re betting that most data will come from a file, so this will be our focus here.

In our case, we have a dump of JSON data with zero formatting applied.

A dump of JSON content.

This file – addresses.json – can be brought into Python and formatted. In order to work with JSON files in Python, you need to import the json module. Then, you can define the file itself as a variable.

Defining a file in Python.

Once you’re at this point, you can begin working with “addresses.json”.

2. Open the JSON File Using Python

Opening and loading a file within Python.

Next, you’ll need to open “addresses.json”. This is done through the usual with open…as method:

with open(json_file, 'r') as file:
…

Within this block of code, you’ll then need to call json.load() on “addresses.json” and assign it to a variable:

parsed = json.load(file)

If you run this file now, it will execute, but you won’t see any result in the Terminal. For this, you’ll need to move onto the third step.

3. Pretty Print Your JSON File (And Adjust the Settings)

The final aspect of pretty printing your JSON file is to perform the print itself. To do this, add the following as an indented line of your with open…as code block:

print(json.dumps(parsed, indent=4, sort_keys=True))

To break this down, the print function calls json.dumps(). This applies three arguments:

  • The name of the file you want to pretty print.
  • The size of the indent.
  • Whether the JSON file should be sorted by its keys.

When you’re ready, save your changes, then open a new Terminal window. Here, you’ll want to navigate to the correct project folder, and run the program.

Using ther Terminal to run your program.

The Terminal output will apply your settings and pretty print the JSON file.

Terminal output showing pretty printed data.

Of course, you would usually store the pretty JSON elsewhere rather than print the data to the Terminal. Even so, there’s a one-line snippet you can use to pretty print a JSON file from the command line.

How to Pretty Print a JSON File in Python from the Command Line

There’s a super-quick way to pretty print JSON files in Python direct from the command line as Terminal output. To do this, open a new Terminal window, navigate to your project folder, and run the following:

cat addresses.json | python3 -m json.tool

In this example, you’ll want to change “addresses.json” to your own filename. Once you run this, you’ll see the Terminal display-formatted output.

This is a great, no-code solution to pretty print JSON files in Python on the fly and takes seconds to run.

Wrapping Up

There are a number of uses for pretty printing a JSON file in Python. Often, you’ll want to make minified data readable, maybe to export to other users. Regardless of your reason, Python lets you do this using the json module and a “with open…as” code block.

If you’re interested in what else Python can do for you, check out our other articles on Python looping and the range() function. Will this method help you in your daily work, and if so, how? Let us know in the comments section below!

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox