How to Pretty Print a JSON file in Python?

The dumps() from the json module & pprint() from the pprint module are used to pretty print a JSON file in Python, JSON data is often stored in a compact, single-line format that can be difficult to read and understand, pretty printing is a technique that formats JSON data with indentation and line breaks to make it more human-readable.

In this article, we will learn different ways to pretty-print a JSON file in Python, using built-in modules and third-party libraries with examples.

To make things clear, let’s have a Sample JSON for the examples. We will use this JSON throughout all our examples. It is a raw JSON that needs some beautification.


# Sample JSON data
{"books":[{"title":"To Kill","author":"Harper Lee","year":1960,"pages":281},
{"title":"To Live","author":"George Orwell","year":1949,"pages":328}]}

1. Quick Examples

These are some of the quick examples that will give you a high-level idea of how to pretty print a JSON file. We will discuss each of them in detail.


# Quick examples of pretty print JSON file

# Method 1 : Using dumps() method
import json
print(json.dumps(json_data, indent=4))

# Method 2 : Using pprint module
import pprint
pprint.pprint(json_data)

# Method 3 : Using rich for coloring
from rich import print
from rich import pretty
pretty.install()
print(json_data)

2. Use json.dump() to pretty print JSON File

The json.dump() method is used to serialize Python objects into JSON format and write them to a file in a pretty print format. By default, the output JSON is compact and hard to read.

You can make the JSON file more readable by using the indent parameter in the json.dump() method. The indent parameter specifies the number of spaces to use for indentation.

Steps to Pretty print a JSON file in Python

  1. Open the JSON file
  2. Load the JSON
  3. Pretty print it using Indentation
  4. Write it back to the file

See the below example:


# Import JSON module
import json

# Read JSON data from file
with open('file.json', 'r') as f:
    data = json.load(f)

# Pretty-print JSON data and write back to file
with open('file.json', 'w') as f:
    json.dump(data, f, indent=4)

This will read the JSON from a file and write it back to the JSON file in a pretty print format.

3. json.dumps() – Pretty Print JSON to Console

What if you don’t want to write it back to the file and instead want to output it to the JSON pretty print string to the Python console? You can use the json.dumps() function to output it to the console instead of the file.

Assume that we want to read JSON from a file and print it to the console. We can use the following code.


# Import json module
import json

# Using dumps()
# Json_date is the above sample JSON
print(json.dumps(json_data, indent=4))

Yields the below output:


# Output:
{
    "books": [
        {
            "title": "To Kill",
            "author": "Harper Lee",
            "year": 1960,
            "pages": 281
        },
        {
            "title": "To Live",
            "author": "George Orwell",
            "year": 1949,
            "pages": 328
        }
    ]
}

4. pprint Module – JSON in Readable format

The pprint module is a Python built-in module that provides the pprint function for pretty-printing Python data structures including JSON. Though it is more general and can be used for other data structures such as dictionaries or lists, pprint can also be used to pretty-print JSON data.

See the below Example:


# Import necessary modules
import json
from pprint import pprint

# Read JSON data from file
with open('example.json', 'r') as f:
    data = json.load(f)

# Pretty-print JSON data using pprint
pprint(data)

Yields below output:


# Output:
{'books': [{'author': 'Harper Lee',
            'pages': 281,
            'title': 'To Kill',
            'year': 1960},
           {'author': 'George Orwell',
            'pages': 328,
            'title': 'To Live',
            'year': 1949}]}

5. pprintjson Library – Pretty print with colors

As we discussed earlier, pprint is more general, which leaves us to the pprintjson library. It is a third-party library that extends the functionality of the built-in pprint module to support JSON data specifically. It provides an alternative syntax to the pprint module that is specifically designed for printing JSON data.

You can specify the number of spaces to use for indentation, or you can choose to sort the keys of the JSON object alphabetically.


# Import necessary modules
import json
from pprintjson import pprintjson

# Read JSON data from file
with open('example.json', 'r') as f:
    json_data = json.load(f)

# Pretty-print JSON data using pprintjson
pprintjson(json_data)

Yields the below Output:

python pretty print json
Output of the code

6. Summary and Conclusion

In this article, we have learned how to pretty print a JSON file in python in different ways. Among these all methods, the simpler method is to use the json.dump() method, However, I personally liked the pprint module. I hope this article was helpful. Ask questions in the comment sections.

Happy Coding!!!

AlixaProDev

I am an astute software engineer with extensive 3+ years of experience in developing full-stack web applications. My skillets include building backend services for different databases. Mainly I work with Python, Flask, Django, and FastAPIs. Being Python Specialist, I have worked with Numpy, Pandas, and other Python Libraries to build tools that make ETL normalization easier.

Leave a Reply