JSON Pretty Print in Python
In this article, we will study how ti Pretty Print JSON in Python. Let us first understand what is JSON.
Pretty Print is the process of displaying source code in an attractive and more readable way.
Let us now create a JSON file. A JSON file is a file that stores data in JavaScript Object Notation (JSON) format.
Step 1: Open a file
Open a blank file and write JSON data into the file. Look at the following JSON code:
[ { "Name": "Rani", "Age" : 36, "Designation": "Python Developer", "Project": "Cancer Detection", "Total Months": 4 }, { "Name": "Shruti", "Age" : 26, "Designation": "PHP Developer", "Project": "Hospital Management", "Total Months": 3 }, { "Name": "Dheeraj", "Age" : 29, "Designation": "Data Analysis", "Project": "Cancer Analysis", "Total Months": 5 } ]
Step 2: Save the file
Once you write the JSON code in blank file, save the file with “.json”. Let’s save the above file as “employee_json_file.json”.
Python program for JSON Pretty Print
Let us now look at way to pretty print JSON data.
Using pprint module
The pprint is a module in Python. It is used to pretty print Python Data Structures. Let us see how to pretty print JSON data using pprint.
Step (i): Import JSON module
To work with JSON data in Python, we need to import JSON module. Look at the following line:
import json as j
Step (ii): Import pprint module
Since our aim is to print JSON data in an attractive way, we need to import pprint module. Look at the following line:
import pprint
Step (iii): Open JSON file
Next step is to open the JSON file. To read a file containing JSON object, we will use “json.load()”. Look at the following code:
with open('employee_json_file.json', 'r') as f: json_data = j.load(f) print(json_data)
OUTPUT
[{'Name': 'Rani', 'Age': 36, 'Designation': 'Python Developer', 'Project': 'Cancer De tection', 'Total Months': 4}, {'Name': 'Shruti', 'Age': 26, 'Designation': 'PHP Devel oper', 'Project': 'Hospital Management', 'Total Months': 3}, {'Name': 'Dheeraj', 'Age ': 29, 'Designation': 'Data Analysis', 'Project': 'Cancer Analysis', 'Total Months': 5}]
Step (iv): Formatting
1. Using pformat: We will now format the JSON data using “pformat”. Look at the following code:
x = pprint.pformat(json_data, indent=4) print(x)
Here, json_data is the variable which has JSON data. In this case, we are passing JSON data and indent for formatting. However, we can also pass width and depth. It will print the formatted representation of JSON.
OUTPUT
[ { 'Age': 36, 'Designation': 'Python Developer', 'Name': 'Rani', 'Project': 'Cancer Detection', 'Total Months': 4}, { 'Age': 26, 'Designation': 'PHP Developer', 'Name': 'Shruti', 'Project': 'Hospital Management', 'Total Months': 3}, { 'Age': 29, 'Designation': 'Data Analysis', 'Name': 'Dheeraj', 'Project': 'Cancer Analysis', 'Total Months': 5}]
2. Using pprint: We can also format the JSON data using “pprint”. Look at the following code:
pprint.pprint(json_data, indent=4)
It will print the formatted representation of JSON.
OUTPUT
[ { 'Age': 36, 'Designation': 'Python Developer', 'Name': 'Rani', 'Project': 'Cancer Detection', 'Total Months': 4}, { 'Age': 26, 'Designation': 'PHP Developer', 'Name': 'Shruti', 'Project': 'Hospital Management', 'Total Months': 3}, { 'Age': 29, 'Designation': 'Data Analysis', 'Name': 'Dheeraj', 'Project': 'Cancer Analysis', 'Total Months': 5}]
Thank You.
You may also read: How to convert DataFrame into List using Python?
how great concept..