Python JSON Pretty Print – JSON Formatting with Python

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.

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.

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.

We can pretty print JSON data easily  in python using json module.For this we have to write the following code.

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.

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.

Now write the following code –

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.

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

Leave a Comment