Skip to content

Python print JSON pretty | Example code

  • by

Use JSON dump() method with indent parameter to print JSON pretty in Python. You should use the optional argument indent.

print(json.dumps(dict, indent=4))

Python pretty print JSON example

Simple example code Python’s built-in JSON module can handle that for you:

import json

data = '[{"ID":101,"Name":"John","Role":"Member"},' \
       '{"ID":102,"Name":"Kenny","Role":"Editor"}]'

res = json.loads(data)

json_str = json.dumps(res, indent=2)

print(json_str)

Output:

Python pretty print JSON example

Do comment if you have any doubts or suggestions on this Pytho pretty print topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading