Hey everyone welcome to my new tutorial Python JSON Pretty Print. Often times JSON data is not formatted so it’s hard to read and that’s why we need the pretty printed. So let’s start to learn how to pretty print JSON data in python.
I have already uploaded a tutorial of reading JSON data. You can check Python Read JSON File – Reading JSON in Python .
Python JSON Pretty Print Tutorial – Getting Started
There are several ways to pretty print JSON data and i am going to show them one by one. But before going to do that, you might have a question – Why pretty print is needed? So let’s take a look why JSON pretty print is important.
Why We Use JSON Pretty Print
JSON pretty print is necessary because sometime you have long JSON response and you may not be able to parse it unless you visualize it on the console that – How the JSON tree is ?
Let’s consider an example of JSON data which is not formatted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
{'status': 'OK', 'results': [{'address_components': [{'long_name': 'Rajpath', 'types': ['route'], 'short_name': 'Rajpath'}, {'long_name': 'India Gate', 'types': ['political', 'sublocality', 'sublocality_level_1'], 'short_name': 'India Gate'}, {'long_name': 'New Delhi', 'types': ['locality', 'political'], 'short_name': 'New Delhi'}, {'long_name': 'New Delhi', 'types': ['administrative_area_level_2', 'political'], 'short_name': 'New Delhi'}, {'long_name': 'Delhi', 'types': ['administrative_area_level_1', 'political'], 'short_name': 'DL'}, {'long_name': 'India', 'types': ['country', 'political'], 'short_name': 'IN'}, {'long_name': '110001', 'types': ['postal_code'], 'short_name': '110001'}], 'geometry': {'location': {'lng': 77.2295097, 'lat': 28.612912}, 'viewport': {'northeast': {'lng': 77.2308586802915, 'lat': 28.6142609802915}, 'southwest': {'lng': 77.22816071970848, 'lat': 28.6115630197085}}, 'location_type': 'APPROXIMATE'}, 'types': ['establishment', 'point_of_interest'], 'formatted_address': 'Rajpath, India Gate, New Delhi, Delhi 110001, India', 'place_id': 'ChIJC03rqdriDDkRXT6SJRGXFwc'}]} |
As you can see this data is very difficult to read so we need to print them in formatted way so that we can easily visualize data. After pretty print, the same data is look like this which is nicely organized and eye catchy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
{'results': [{'address_components': [{'long_name': 'Rajpath', 'short_name': 'Rajpath', 'types': ['route']}, {'long_name': 'India Gate', 'short_name': 'India Gate', 'types': ['political', 'sublocality', 'sublocality_level_1']}, {'long_name': 'New Delhi', 'short_name': 'New Delhi', 'types': ['locality', 'political']}, {'long_name': 'New Delhi', 'short_name': 'New Delhi', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'Delhi', 'short_name': 'DL', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'India', 'short_name': 'IN', 'types': ['country', 'political']}, {'long_name': '110001', 'short_name': '110001', 'types': ['postal_code']}], 'formatted_address': 'Rajpath, India Gate, New Delhi, Delhi ' '110001, India', 'geometry': {'location': {'lat': 28.612912, 'lng': 77.2295097}, 'location_type': 'APPROXIMATE', 'viewport': {'northeast': {'lat': 28.6142609802915, 'lng': 77.2308586802915}, 'southwest': {'lat': 28.6115630197085, 'lng': 77.22816071970848}}}, 'place_id': 'ChIJC03rqdriDDkRXT6SJRGXFwc', 'types': ['establishment', 'point_of_interest']}], 'status': 'OK'} |
And now we will learn how to pretty print JSON data in python. So let’s start.
Creating a New Project
First start your PyCharm IDE. I use this IDE but if you don’t use this then it’s ok as it’s not a big deal. Although you want to use PyCharm then check this link to learn how to download and install PyCharm? And if you want to know about best python IDE then this link is useful for you.
Now go to your IDE and create a new project and inside the project create a python file. In my case my project is like this –
Finally we have created new project successfully and now it’s time to start coding. So let’s move ahead before wasting time.
Python JSON Pretty Print Using JSON module
First of all we have to install requests module. Go to terminal and run following command.
1 2 3 |
pip install requests |
We can pretty print JSON data easily in python using json module.For this we have to write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 |
import json import requests # getting data from source get_data = requests.get("https://reqres.in/api/users?page=2") # pretty printing data pretty_data = json.dumps(get_data.json(), indent=4) print(pretty_data) |
Code Explanation
- First of all we have to import json and requests module.
- Then request the JSON data using requests.get() method.
- Most of the time JSON response have a default type dictionary. So in this case we use json.dumps() method and pass the json data and indent as parameters.
- Indent is nothing but used for pretty printing JSON data. If indent is a non-negative integer or string, then JSON array elements and object members will be pretty-printed with that indent level.
- In this case i am using indent level 4.
- And then simply print the data.
Now the output will be look like –
Python JSON Pretty Print Using pprint module
- The
pprint
module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. - If the formatted structures include objects which are not fundamental Python types, the representation may not be loadable.
- This may be the case if objects such as files, sockets, classes, or instances are included, as well as many other built-in objects which are not representable as Python constants.
For pretty print using pprint module we have to write following code.This is quite simple.
1 2 3 4 5 6 7 |
from pprint import pprint import requests get_data = requests.get("https://reqres.in/api/users?page=2") pprint(get_data.json()) |
The output of this code is –
As you can see it is not as good as the JSON module but you can still visualize data.
Python JSON Pretty Print Using ipdb module
The one more option of pretty print in python is that if you are using Ipython debugger then ipdb module provide you to do pretty print of JSON data.
- ipdb is a Python Debugger i.e., IPython-enabled .
- It is a third party interative debugger and also having all pdb’s functionality.
- It adds IPython support for the interactive shell, like tab completion, color support, magic functions and much more.
For pretty print using ipdb we have to write following code. But first install ipdb module by running following command on terminal.
1 2 3 |
pip install ipdb |
Now write the following code –
1 2 3 4 5 6 7 |
import requests import ipdb get_data = requests.get("https://reqres.in/api/users?page=2") ipdb.set_trace() |
If you will run this code then you will get an interactive debugging console look like below –
Now we have to pretty print data, for this enter the following code in console.
1 2 3 |
ipdb> pp get_data.json() |
The output will be as follows –
So these ways we can pretty print JSON data in python and it is not so hard.
So this was all about the Python JSON Pretty Print Tutorial. If you have any queries regarding this post then just leave your comment. And if you found it as useful then please share as much as possible. Thanks