This post will discuss how to pretty-print a JSON file in Python.

1. Using pprint.pprint() function

The most common approach to pretty-print a JSON file in Python is using the built-in module pprint, which provides the capability to print the formatted representation of JSON data.

Download Code

2. Using json.dump() function

Another good option to pretty-print a JSON file is using the json.dumps() function, which by default returns the most compact representation of json. For pretty-printing the JSON, you can specify the desired indentation using the indent parameter.

 
1. A positive indent level indents that many spaces per level.

Download Code

 
2. If the indent is a string (such as '\t'), that string will indent each level.

Download Code

 
3. A zero or negative indent level will only insert newlines.

Download Code

3. Using simplejson.dump() function

You can also use the simplejson module that works similarly to the json module.

Download Code

That’s all about pretty printing a JSON file in Python.