How to Pretty Print a JSON file in Python
In this article, we will learn how to pretty print a JSON file in Python. We will use some built-in functions available in Python for JSON and some related custom examples as well. Let's first have a quick look over what is JSON and what is Pretty Print in Python.
What is JSON?
JSON stands for JavaScript Object Notation
. It is a popular data format used for representing structured data. It is a lightweight format that is used for data interchanging. The data representation in JSON is similar to that of Python Dictionary. It is a collection of name/value pairs. In JSON, it is common to transmit and receive data between a server and web application in JSON format. It is also common to store a JSON object in a file. JSON data can be in the form of the object, array, value, string, or number.
JSON Example
data = '{"model number": "RX234", "customers": ["Adam", "Paul"], “price”: 45000, “quantity”: 12, “company”: “Samsung”}'
Pretty Print a JSON file
Whenever we convert a Python object into a JSON object, the output of printing the JSON object is displayed the same as the Python dictionary. JSON module in Python suggests json.dumps()
function to convert Python object to JSON object. The JSON pretty print concept is introduced here to display the JSON object in a good format and in a readable and presentable way. JSON Pretty Print is required generally for testing, analyzing, and debugging JSON data.
The below two examples will show how normal JSON data is converted to human-readable form by applying pretty print to JSON data. Pretty print a JSON data provides the proper indentation and adds new lines to improve readability.
Example: A Normal JSON data
The below code takes JSON data and prints the output with no proper spaces. The output is displayed in one single line. Although it is not complicated but still poorly formatted.
import json
json_data = '[{"model number":"RX100","customer":"Adam","Company":"Samsung"},'\
'{"model number":"FH450","customer":"Paul","Company":"Lenevo"}]'
#loads() load the data to object
json_object =json.loads(json_data)
#prints the data in one line
print(json.dumps(json_object))
[{"model number": "RX100", "customer": "Adam", "Company": "Samsung"}, {"model number": "FH450", "customer": "Paul", "Company": "Lenevo"}]
Example: Pretty Print the JSON data
The below example takes indent
keyword while dumping the data decides. It decides what level spaces the user wants. The output is displayed with the difference in the spaces near the brackets and with proper indentation.
import json
json_data = '[{"model number":"RX100","customer":"Adam","Company":"Samsung"},'\
'{"model number":"FH450","customer":"Paul","Company":"Lenevo"}]'
#loads() load the data to object
json_object =json.loads(json_data)
#prints the data with proper indentation
print(json.dumps(json_object, indent=2))
[
{
"model number": "RX100",
"customer": "Adam",
"Company": "Samsung"
},
{
"model number": "FH450",
"customer": "Paul",
"Company": "Lenevo"
}
]
Pretty Print a JSON file on the command line
You can also pretty-print a JSON file using oneliner on the command line. It is a really easy way to pretty print your JSON by leveraging the Python command-line binary. No coding is required. The below command is used. It pipes the data to Python and applies the JSON tool.
cat data.json | python -m json.tool
Conclusion
In this article, we learned how to pretty print a JSON file using json.dumps()
function. We can even increase the value of the indent
keyword to see changes in the output. We also discussed a single command that can be used on the Python command line to perform pretty print.