How to pretty print JSON objects in Python
Python is a dynamic high-level programming language. Built-in libraries and functions make it the go-to language for developers looking for rapid application development.
In this answer, we'll discuss how to pretty print JSON objects.
JSON objects
We use JavaScript Object Notation (JSON) to represent data in a human-readable format. A JSON object is represented with curly brackets, and data is represented in a key-value pair format. The syntax of a JSON object is given below:
{"key1" : "value1","key2" : "value2","key3" : "value3",....}
As represented in the code block, the key is on the left side of the :, and the value is on the right side. Now, let's see what JSON arrays are.
JSON arrays
We come across JSON arrays when we receive a response from a web API or deal with JSON files. A JSON array is a collection of JSON objects where a comma separates each JSON object. The syntax of a JSON array is given below:
[{"key1" : "value1","key2" : "value2","key3" : "value3",....},{"key5" : "value5","key6" : "value6","key7" : "value7",....},....]
However, the problem is that the response we get from a web API needs to be in a readable format. As shown below, the response is usually not well-formatted and does not have spaces or line breaks for easy readability:
[{"id":1,"name":"John"},{"id":2,"name":"Emily"},{"id":3,"name":"Michael"},{"id":4,"name":"Sophia"},{"id":5,"name":"Daniel"},{"id":6,"name":"Olivia"},{"id":7,"name":"James"},{"id":8,"name":"Emma"},{"id":9,"name":"Alexander"},{"id":10,"name":"Isabella"},{"id":11,"name":"William"},{"id":12,"name":"Ava"},{"id":13,"name":"Benjamin"},{"id":14,"name":"Mia"},{"id":15,"name":"Henry"},{"id":16,"name":"Sophie"},{"id":17,"name":"Joseph"},{"id":18,"name":"Ella"},{"id":19,"name":"Samuel"},{"id":20,"name":"Grace"},{"id":21,"name":"Jacob"},{"id":22,"name":"Lily"},{"id":23,"name":"Matthew"},{"id":24,"name":"Chloe"},{"id":25,"name":"David"}]
Now, imagine if we have thousands of objects in the array. It will become a tiresome task to read and extract data from them. Therefore, in the coming sections, we will see how to convert our JSON arrays and objects into a readable format by prettifying them.
Pretty format
The pretty format is a practice that we use to convert the JSON objects into a readable format by adding appropriate indents, line breaks, and spaces. The pretty format of the previous example is shown below:
[{"id": 1,"name": "John"},{"id": 2,"name": "Emily"},{"id": 3,"name": "Michael"},{"id": 4,"name": "Sophia"},{"id": 5,"name": "Daniel"},{"id": 6,"name": "Olivia"},{"id": 7,"name": "James"},{"id": 8,"name": "Emma"},{"id": 9,"name": "Alexander"},{"id": 10,"name": "Isabella"},{"id": 11,"name": "William"},{"id": 12,"name": "Ava"},{"id": 13,"name": "Benjamin"},{"id": 14,"name": "Mia"},{"id": 15,"name": "Henry"},{"id": 16,"name": "Sophie"},{"id": 17,"name": "Joseph"},{"id": 18,"name": "Ella"},{"id": 19,"name": "Samuel"},{"id": 20,"name": "Grace"},{"id": 21,"name": "Jacob"},{"id": 22,"name": "Lily"},{"id": 23,"name": "Matthew"},{"id": 24,"name": "Chloe"},{"id": 25,"name": "David"}]
Python provides us with a json module to pretty print the JSON objects. In the following code, we can see how to pretty print our JSON arrays:
import json# JSON data string containing an array of objectsjsonData = '[{"Id":10,"Name":"James","Role":"CEO"},' \'{"Id":20,"Name":"David","Role":"Editor"}]'# Parsing the JSON data string into Python objectsjsonObject = json.loads(jsonData)# Converting the Python objects back to a JSON string with indentationjsonPrettyFormattedStr = json.dumps(jsonObject, indent=3)# Printing the prettified JSON stringprint("Pretty formatted JSON array: " , jsonPrettyFormattedStr)
The code explanation is given below:
Line 1: We import the
jsonmodule that provides the functionalities to work with JSON data.Line 4: We create a
jsonDatavariable with a hard-coded JSON array.Line 8: We use the
loads()function from thejsonmodule. The function helps to convert ourjsonDatastring to a Python object and store it injsonObject.Line 11: We use the
dumps()function from thejsonmodule and convertjsonObjectinto a JSON string with an indentation of 3 spaces.Line 14: We print our prettified JSON array.
This way, we can convert our non-readable JSON array of objects into well-structured and indented format.
Free Resources