How to pretty print a JSON file in Python?

Ads
 

How to pretty print a JSON file in Python?

In this tutorial we are going to read a json file and then pretty print the JSON content of file on the console. You can extend the program to save the formatted JSON to another file.

In this tutorial we are going to read a json file and then pretty print the JSON content of file on the console. You can extend the program to save the formatted JSON to another file.

How to pretty print a JSON file in Python?

In this tutorial I will teach you to read the JSON file in Python and then pretty print the content on the console, which a nice way to see the formatted content of the JSON data. This is useful when you have a big JSON file which is not formatted and you want to check the content of the file. In such scenario you can read the JSON file in Python, format the JSON and print the formatted content. The formatted content can be easily viewed by humans.

In this tutorial I will read an unformatted JSON file in Python, then use the Python function to format the JSON and print on the console. You can also save the formatted content to another file if you have to read it separately. This is useful if you want to send the formatted JSON file to the testing team for validating the data during the manual testing process.

In Python programming people are using json library to work with the JSON files in many different ways. Several times it is easy to pretty print the JSON in Python using various JSON parsing libraries available in Python.

Step 1: Reading the JSON file in Python

Suppose you have a file called myjson.json with following content:


{"1": "Sunday", "2": "Monday", "3": "Tuesday", "4": "Wednesday", "5": "Thursday", "6": "Friday", "7": "Saturday"}

And your requirement is to read the content of the file in Python and then format the JSON for pretty printing on console for easy debugging. Then you can use the json library of Python for formatting the content. If you have any other file with then you can use the program here to read and test the program.


import json

# read JSON file
with open('myjson.json', 'r') as jsonfile:
    json_data=jsonfile.read()

print(json_data)

Above code prints the content of JSON file, but it is not formatted:

JSON file 

Step 2: Pretty print JSON file data

Now we can first parse the json with with json.loads() function and finally convert it to pretty format with the help of json.dumps(data_json, indent=4)  function. Here is complete code of pretty print file JSON on the console:


import json

#read JSON file
with open('myjson.json', 'r') as jsonfile:
    json_data=jsonfile.read()

print(json_data)

#parse Json
data_json = json.loads(json_data)

#pretty print json
pretty_json = json.dumps(data_json, indent=4)  
print(pretty_json) 

If you run the code you will get following output:

Python pretty print example 

Output of the program:


>>> import json
>>> 
>>> #read JSON file
... with open('myjson.json', 'r') as jsonfile:
...     json_data=jsonfile.read()
... 
>>> print(json_data)
{"1": "Sunday", "2": "Monday", "3": "Tuesday", "4": "Wednesday", "5": "Thursday", "6": "Friday", "7": "Saturday"}

>>> 
>>> #parse Json
... data_json = json.loads(json_data)
>>> 
>>> #pretty print json
... pretty_json = json.dumps(data_json, indent=4)  
>>> print(pretty_json) 
{
    "1": "Sunday",
    "2": "Monday",
    "3": "Tuesday",
    "4": "Wednesday",
    "5": "Thursday",
    "6": "Friday",
    "7": "Saturday"
}
>>> 

Step 3: Pretty printing with json.tool

You can use python and json.tool for pretty printing the json file content from the terminal. Open the terminal and run following command

python3 -m json.tool myjson.json

Above command will pretty print the content of the json file file as shown below:

Pretty print json file 

In this tutorial we have learned the examples of pretty printing the JSON file using Python library.

Here more examples of Python:

Ads

Ads