How to Pretty Print a JSON file in Python? – Spark By {Examples}

The dumps () of the json module and pprint() of the pprint module are used to print a JSON file in Python, the 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 the JSON data with indentation and line breaks to make it more readable by humans.

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

To clear things up, let’s have an example JSON for the examples. We will use this JSON in all our examples. It’s a raw JSON that needs a bit of 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

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

# Quick examples of beautiful print JSON file # Method 1: Using the dumps() method import json print(json.dumps(json_data, indent=4)) # Method 2: Using the pprint import module pprint pprint.pprint(json_data) # Method 3: Using rich to color from rich import print from pretty rich import.install() print(json_data)

2. Use

json.dump() to print the JSON file quite a bit

The json.dump() method is used to serialize Python objects in JSON format and write them to a file in a nice print format. By default, output JSON is compact and difficult 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 print a

JSON file in Python Open the JSON file Load the JSON

  1. Pretty print it using indentation
  2. Rewrite it
  3. to the file See the

following example

: # Import JSON module Import JSON # Read JSON data from file with open(‘file.json’, ‘r’) as f:data = json.load(f) # Print nice 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 nice 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 send it to the pretty JSON print string in the Python console? You can use the json.dumps() function to send it to the console instead of the file.

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

# Import json import module json # Using dumps() # json_date is the previous example JSON print(json.dumps(json_data, indent=4))

Produces the following output

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

4. pprint Module

– JSON in readable format The pprint module

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

See the following

example: # Import the required modules Import JSON from Pprint Import Pprint # Read JSON data from file with open(‘example.json’, ‘r’) as f: data = json.load(f) # Print beautiful JSON data using pprint pprint (data)

Yields below output

: # Output: {‘books’: [{‘author’: ‘Harper Lee’, ‘pages’: 281, ‘title’: ‘Kill’, ‘year’: 1960}, {‘author’: ‘George Orwell’, ‘pages’: 328, ‘title’: ‘Live’, ‘year’: 1949}]} 5. pprintjson Library

– Nice color printing

As we discussed earlier, pprint is more general, which leaves us with 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 in the JSON object alphabetically.

# Import the 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) # Print JSON data using pprintjson pprintjson(json_data)

Produces the following output:

6. Summary and conclusion

In this article, we have learned how to print a JSON file in Python in different ways. Among all these methods, the simplest 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 comments sections.

Happy coding!!!

Contact US