JSON
or JavaScript Object Notation
is a structured file format popularly used in JavaScript. JSON is used to store data in a simple and structured way that can be also easily readable by humans. Today JSON is popularly used to transmit data between different parties like client and server. JSON may store simple data and complex data which consist of 300-500 entities. Pretyprinting means displaying the JSON data in a more readable, hierarchically indented way.
dumps() Method
Python provides jsondumpd() method which is provided by
the json
module. The jsondumps() can be used to print or dump provided JSON formatted data into the terminal. The JSON data can be loaded from a file or accepted as a parameter. The dumps() method accept different parameters but the JSON data object and indent are required to pretty print. The indent
parameter is used to set indent for every level of the JSON data. In the following example we use indent as 4 which means 4 spaces for every level.
# Import json library to use dumps() method
import json
# Initialize JSON data
json_data = '[ {"studentid": 1, "name": "john", "subjects": ["Math", "Science"]}, {"studentid": 2, "name": "betty", "subjects": ["biology", "Operating System"]} ]'
# Create Python object from JSON data
obj = json.loads(json_data)
# Pretty Print JSON with dumps() Method
json_formatted_str = json.dumps(obj,indent=4)
print(json_formatted_str)

[ { "studentid": 1, "name": "john", "subjects": [ "Math", "Science" ] }, { "studentid": 2, "name": "betty", "subjects": [ "biology", "Operating System" ] } ]
As we can see from the output it is indented properly and very easy to read in a hierarchical manner.
pprint() Method
Python also provides the pprint
module where the pprint()
method can be used to print JSON data in a pretty and more readable way. The pprint() method prints provided JSON data in a more compact way which is a bit different from the dumps() method described previously.
# Import json and pprint modules
import json
import pprint
# Initialize JSON data
json_data = '[ {"studentid": 1, "name": "john", "subjects": ["Math", "Science"]}, {"studentid": 2, "name": "betty", "subjects": ["biology", "Operating System"]} ]'
# Create Python object from JSON data
obj = json.loads(json_data)
pprint.pprint(obj)

Pretty Print via Command Line Interface
The command-line interface like Linux Bash, Windows MS-DOS, or PowerShell can be used to print a JSON file and its contents to the command line interface in a pretty way. The python3 command is used with the json.tool module and the name of the JSON file are provided.
$ python3 -m json.tool students.json

Pretty Print with pygmentize via Command Line Interface
The pygmentize
is a 3rd party Python module used to print given JSON in a pretty format with coloring. The pygmentize is not installed by default. So first we install it with the package manager apt like below. The pygmentize package is named as python3-pygmentize
.
$ sudo apt install python3-pygments
Now we can use the pygmentize like below. The -l
option is used to specify the language of the data which is json
in this example. The json.tool
is used to set indents etc. The pygmentize command sets colors for the data according to the JSON syntax.
$ cat students.json | python3 -m json.tool | pygmentize -l json
