Pretty print a JSON file in Python

People use the JSON format to store and read data easily. By default, Python sends a minified version of Python, to save memory and bandwidth. But when you try to debug, it’s easier to read the beautified version of a JSON file.

In this lesson, I’m going to show how to save a JSON file, how to display, and what is most important, how to pretty print it on the console.

This code creates a JSON file – “file.json” and write the json_write list to a file.

Let’s display json data using the following code:

If you try to display this data, you are going to get the following output:

Pretty print JSON

It’s time to display the JSON file in beautified mode. This code is very similar to the previous one, where we displayed the result, but this time we are going to set additional parameters in json.dumps.

This code will display the following result.

sort_keys = True means that the output of dictionaries will be sorted by key and the indent is 4.