Python JSON pretty print

JSON (JavaScript Object Notation) is a widely used format to store the data. It is used to exchange data between servers and software applications, i.e., web application, mobile application, etc. Python provides a built-in JSON module to perform JSON related tasks.

The Python object (i.e., list, dictionary, tuple) can be converted into JSON. We use the dumps() function from the JSON module to perform this conversion. Whenever the Python object is converted into a JSON, and if we print the converted JSON object, results will be displayed the same as the Python dictionary. The JSON pretty print refers to display the JSON object in a good format and presentable way.

This article explains the JSON pretty print concept with the help of examples.

The json.dump() function

As discussed previously, the json.dump() is a built-in Python function that converts the Python object into JSON format. Let’s convert a Python dictionary object into JSON format.

#importing json module
import json
#creating a Python dictionary object
my_dict = {"name":"David","age":30,"email":"david@example.com","coutry":"USA"}
#converting to JSON format
result_json = json.dumps(my_dict)
#printing the converted json object
print(result_json)

Output

The Python dictionary object is successfully converted to the JSON format.

The output seems like a Python dictionary. It is not in a good JSON format and presentable way. It is not prettified. The “indent” property is used inside the json.dumps() function to present the JSON data in a proper presentable format with space. Let’s use the indent property with the json.dumps() function. The “indent=1” adds the one space in JSON data.

#importing json module
import json
#creating a Python dictionary object
my_dict = {"name":"David","age":30,"email":"david@example.com","coutry":"USA"}
#converting to JSON format
result_json = json.dumps(my_dict,indent=1)
#printing the converted json object
print(result_json)

Output

Now the output is prettified, and JSON data is presented in the proper format.

As we keep increasing the number of indents, the spaces will increase in the data.

#importing json module
import json

#creating a Python dictionary object
my_dict = {"name":"David","age":30,"email":"david@example.com","coutry":"USA"}
#converting to JSON format
result_json = json.dumps(my_dict,indent=5)
#printing the converted json object
print(result_json)

Output

#importing json module
import json
#creating a Python dictionary object
my_dict = {"name":"David","age":30,"email":"david@example.com","coutry":"USA"}
#converting to JSON format
result_json = json.dumps(my_dict,indent=10)
#printing the converted json object
print(result_json)

Output

Python pretty print JSON file

Now, let’s try to open a JSON file and display it in pretty print format. The json.loads() function parse the JSON data.

#importing json module
import json
#opening and reading the json file
with open('example.json', 'r') as json_result:
    json_data = json.load(json_result)
#printing the json file without pretty print    
print(json.dumps(json_data))
print('\n')
#printing the json file without pretty print
print(json.dumps(json_data, indent=1))

Output

Conclusion

JSON is a widely used data format to store data and exchange data between servers and software applications. Python has a built-in JSON module to perform JSON related tasks. The JSON pretty print displays the JSON output in a well-formed and presentable way. This article explains the Python JSON pretty print with explains.



from Linux Hint https://ift.tt/2GFpJ0a

Post a Comment

0 Comments