Python Pretty Print JSON

The pprint module adds a capability to “pretty-print” arbitrary Python data structures in a form that can be used as input to the interpreter. In this tutorial, will learn about Python Pretty Print JSON. Also, we will cover these topics.

  • Python Pretty Print JSON to File
  • Python Pretty Print JSON String
  • Python Pretty Print JSON Command Line
  • Python Pretty Print JSON Response
  • Python Pretty Print JSON Object
  • Python Pretty Print JSON Dict
  • Python Pretty Print JSON color
  • Python Pretty Print JSON Dump
  • Pytho Pretty Print JSON One Liner

Python Pretty Print JSON to File

In this section, we will learn about Python pretty print JSON to file conversion.

  • PPrint is a built-in module in python that can be imported directly without any installation.
  • Pretty Print (pprint) module offers wide range of modules that helps in cleaning the data and present it more nicely.
  • Please note that json.dump() method is used to convert python object to json object and the output appears in an organised manner like pprint() method do. But they both are not same.
  • pprint() method prints the json data then and there in an organised manner whereas dump() methods converts the python object to json object data can be written to file.
  • In our example, we have used sample JSON data that is available on boredapi website.

Source Code:

  • Using requests module we have fetched the JSON data from the URL. This URL is from th boredapi.
  • We have used json.dump() to convert the data into json object. Also, dumped data appears like a pretty print (pprint) method.
  • to store the result in a file we have used python open() method.
  • In the end, using write() method we write the data into the file.
import requests
import json

response = requests.get("https://www.boredapi.com/api/activity")
pretty = json.dumps(response.json(), indent=2)
res = open('result.json', mode='w')
res.write(pretty)

Output:

In this output, when we run the code, the information is fetched from the website and converted to a JSON object and a new file is created.

python pretty print json to file
Python Pretty Print JSON to File

Read: Python Screen Capture

Python Pretty Print JSON String

In this section, we will learn about python pretty print JSON string.

  • JSON file look like a python dictionary but it has a ‘str’ datatype which means its a string. So please don’t get confused, simple json file can be called as json string.
  • Using pprint() method in python we can improve the presentation of the JSON string. Regular json string could be in one line without any indentations.
  • In our example, we have created a python dictionary and then converted that python dictionary to JSON object. In the using pprint() module we have printed the data in a pretty print format.

Source Code:

  • In this source code, we have imported two modules. JSON to convert python object to JSON object and pprint to implement pretty print.
  • Using json.loads() method python object is converted to JSON object.
  • In the end, we have used pprint() method from python pprint module using which we can print the result in a prettly format.
import json
import pprint

json_data = '[{"id": 1, "name": "Andrew W Benton", "gender": "male", "race": "Asian", "city": "BELLFLOWER"},'\
'{"id": 2, "name": "Carlena E Cleek", "gender": "female", "race": "white", "city": "PEMAQUID"}]'

json_object = json.loads(json_data)
pprint.pprint(json_object)

Output:

In this output, the python dictionary object is displayed in JSON format with proper indentation applied using the pretty print method.

python-pretty print JSON string
Python Pretty Print JSON String

Read: Python print without newline

Python Pretty Print JSON Command Line

In this section, we will learn how to run python pretty print JSON on the command line.

Suppose you have a JSON file on your machine and you want to view it in a pretty print format then you can do that using the following command.

cat file.json | python3 -m json.tool
  • Here, cat is the linux command use to view the file data. cat file.json this command will display the entire content of file.json on the terminal.
  • python3 -m json.tool this command will help in displaying the file.json data in a pretty print format.
  • If you are windows user then use python instead of python3 and use ‘echo’ inplace of cat.

Output:

In this output, you can see that we have run the pretty print JSON on a terminal.

python pretty print json command line
Python Pretty JSON Command Line

Read: Python print 2 decimal places

Python Pretty Print JSON Response

In the section, we will learn about Python Pretty Print JSON Response.

  • Response and Request are the two terms that works back and through.
  • Everytime we make request from a website or URL, a response is received. No response means website is not working.
  • Using Python Requests module we can get the information from the URL. This information is called Response. If no response is received that means incorrect url is passed.
  • Once we have collected the response we can simply dump the JSON data or we can use pretty print method to make it look clean.
  • Plese note that dump() and pretty print (pprint) method do the same things here but they are not same.

Source Code:

In this code, the requests module is used to fetch the response from the URL and then the data is converted to a JSON object and then presented using a print method of the python print module.

import requests
import json
import pprint

response = requests.get("https://www.boredapi.com/api/activity")
pretty = json.dumps(response.json(), indent=2)
pprint.pprint(pretty)

Output:

In this output, you can see that JSON data is represented in a proper format using pretty print.

python pretty print json response
Python Pretty Print JSON Response

Read: How to print factorial of a number in Python

Python Pretty Print JSON Object

In this section, we will learn about the Python Pretty Print JSON object. Using json.loads() method of JSON module we can create the JSON object.

In the below code first, we have converted the python dictionary object to a JSON object by using json.loads() method then we have printed the object using pprint() method of pprint module.

import json
import pprint

json_data = '[{"id": 1, "name": "Andrew W Benton", "gender": "male", "race": "Asian", "city": "BELLFLOWER"},'\
'{"id": 2, "name": "Carlena E Cleek", "gender": "female", "race": "white", "city": "PEMAQUID"}]'

json_object = json.loads(json_data)
pprint.pprint(json_object)

Output:

In this output, a python pretty print JSON object is displayed.

python-pretty print JSON string
Python Pretty Print JSON Object

Read: How to Print Python Fibonacci series

Python Pretty Print JSON Dict

In this section, we will learn about Python Pretty Print JSON Dict. This method can be used for Python pretty print JSON array as well.

  • JSON and Python Dictionary look similar but both have different data types.
  • JSON object has string datatype (‘str’) and dictionary has ‘dict’ datatype.
  • Using json.loads() method we can convert the python object to JSON object.
  • python pprint method is used to display the data in pretty print format.
import json
import pprint

json_data = '[{"id": 1, "name": "Andrew W Benton", "gender": "male", "race": "Asian", "city": "BELLFLOWER"},'\
'{"id": 2, "name": "Carlena E Cleek", "gender": "female", "race": "white", "city": "PEMAQUID"}]'

json_object = json.loads(json_data)
pprint.pprint(json_object)

Output:

In this output, data is displayed in a tree structure using pprint() method of the python pprint module.

python-pretty print JSON string
Python Pretty Print JSON Dict

Source Code:

In this source code, json_data is a dictionary and we have converted it to JSON using json.load() method and then we have used pprint() to print the JSON data in a Pretty Print format.

import json
import pprint

json_data = {"id": 1, "name": "Andrew W Benton", "gender": "male", "race": "Asian", "city": "BELLFLOWER", "mobile": "217-412-6676"} 
print(type(json_data))

d = json.load(json_data)
pprint.pprint(d)

Output:

In this output, we have demonstrated python pretty print JSON dictionary. We have converted the dictionary data to Python Pretty Print JSON.

python pretty print json dict
Python Pretty Print JSON Dict

Read: Python program to print prime numbers

Python Pretty Print JSON color

In this section, we will learn about Python Pretty Print JSON color.

  • Using Python Rich module we can color the pretty print JSON on the terminal. It uses the color on terminal to make the JSON look nice.
  • Python RICH module provide print_json() method. Anything printed using this method appears colorful on the terminal.
  • Using Python Request module data is fetched from the website. In our case we are using boredapi, it the sample json available on the internet.
  • json.dumps() method converts the normal json to pretty json file which is organised with indentation and spaces.

Source Code:

In this source code, we have used the python requests module to fetch the data from the URL. The print_json() method of the rich module is used to color the output on the terminal.

import requests
import json
from rich import print_json

response = requests.get("https://www.boredapi.com/api/activity")
pretty = json.dumps(response.json(), indent=4)

print(pretty)
print_json(pretty)

Output:

In this output, python pretty print JSON color is displayed. To prove the distinction we have displayed the regular output first and then colorful output.

Python Pretty Print JSON Color
Python Pretty Print JSON Color

Read: Python program to print element in an array

Python Pretty Print JSON Dumps

In this section, we will learn about Python Print JSON Dumps.

  • Python JSON dumps method converts the python object to json object also it displays the data in an organised manner. The data is seperated with indentation and looks very clean.
  • Python JSON Dump purpose is to convert python to json object where as pprint() method provides wide variety of methods to control the look and feel of displayed data.
  • In our example, we have converted a python dictionary data to json data using json.dumps() method.

Source Code:

In this source code, the JSON module is imported and json.dumps() method is used to convert python data to JSON.

import json

# raw json
json_data = {"id": 1, "name": "Andrew W Benton", "gender": "male", "race": "Asian", "city": "BELLFLOWER", "mobile": "217-412-6676"} 

# using dump to pretty json
pretty_json = json.dumps(json_data, indent=2)

# printing result
print(pretty_json)

Output:

In this output, you can see that the information is displayed in a proper format. It has indentation and spaces where ever required. This form of JSON looks more clean and easy to understand.

python pretty print to json dump
Python Pretty Print JSON Dump

Read: Python program to print pattern

Python Pretty Print JSON One Liner

In this section, we will learn about the Python Pretty Print JSON one-liner. At times you have to run the JSON file without opening it into a text or code editor at that time this method can be used. It is a one-line code to view the JSON file using Python.

echo '{"Name":["john", "moly"],"Grade":["A", "C"]}' | python -mjson.tool

You may also like to read the following articles.

In this tutorial, we have learned about Python pretty print JSON. Also, we have covered these topics.

  • Python Pretty Print JSON to File
  • Python Pretty Print JSON String
  • Python Pretty Print JSON Command Line
  • Python Pretty Print JSON Response
  • Python Pretty Print JSON Object
  • Python Pretty Print JSON Dict
  • Python Pretty Print JSON color
  • Python Pretty Print JSON Dump
  • Python Pretty Print JSON One Liner