How to Pretty Print JSON Data in Python?

The JSON format (JavaScript Object Notation) is a lightweight data exchange format that is simple to read, write, and parse by computers. You can work with JSON data in Python using a built-in package called json.

To have smooth work with large datasets, the JSON data is pretty-printed using various functions. 

This post provides various ways to pretty print JSON data in Python. Let’s start with the following methods:

  • Method 1: Using json.dumps() Method
    • Pretty Print the JSON String
    • Pretty Print the JSON File
  • Method 2: Using pprint Function

Method 1: Using json.dumps() Method

A Python object can be serialized into a JSON string utilizing the “json.dumps()” built-in method. The json.dumps() function takes a Python object, such as a dictionary or a list, and returns a JSON string representation of the object. This function prints JSON data pretty using the examples below:

Example 1: Pretty Print JSON String

The following example prints pretty JSON string data:

Code: 

import json
value = json.loads('{"Name":"Joseph","Info":[{"Id":180445},{"Age": 23}]}')
print(value)
pretty_json = json.dumps(value, indent=4)
print('\n',pretty_json)
  • The module named “json” is imported.
  • The “json.loads()” accepts the JSON string as an argument and transforms it into a Python dictionary.
  • The “json.dumps()” function takes the returned value of the “json.loads()” function and indent parameter as an argument and retrieves the pretty print value of JSON string.

Output:

The given JSON string has been pretty-printed using json.dumps() function.

Example 2: Pretty Print JSON File

In this example, the JSON content is stored in a file and then pretty printed. Before digging into details, let’s look at the content of the JSON file named “example.json”:

Code:

import json
with open('example.json', 'r') as jf:
    json_obj = json.load(jf)
print(json_obj)
print('\n',json.dumps(json_obj, indent=4))
  • The module named “json” is imported.
  • The “with open()” function opens the JSON file in the read “r” mode and stores it in object “jf.”
  • The “json.loads()” takes the JSON string object “jf” as a parameter and converts it into a Python dictionary.
  • The “json.dumps()” function accepts Python dictionary returns by the json.loads() function along with the indent parameter to retrieve the pretty print JSON data.

Output:

The JSON file data has been pretty-printed using the json.dumps() function.

Method 2: Using pprint Function

A Python interpreter uses the pprint module to “pretty-print” arbitrary Python data structures. The below code uses the “pprint” module to pretty print JSON data:

Code:

import json
import pprint

with open('example.json', 'r') as f:
    data = f.read()
    print('Original Data: \n', data)
    json_data = json.loads(data)
    print('\nPretty Print Data:')
    pprint.pprint(json_data)
  • The module named “json” and “pprint” is imported.
  • The “open()” function opens the given JSON file in read mode.
  • The “f,read()” function is used to read JSON file data.
  • The “json.loads()” function converts the JSON data into a Python dictionary.
  • The pprint.pprint() is used to print pretty given JSON data.

Output:

The input JSON data has been pretty-printed using the pprint.pprint() function.

Conclusion

The “json.dumps()” method of the JSON module and “pprint.pprint() function“ of the “pprint” module is used to pretty print JSON string and JSON file. First, the “json.loads” parses the data, and then the “json.dumps()” and “pprint.pprint()” functions pretty-print the respective data. This guide has presented different methods to pretty print JSON data with the help of numerous examples.