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](https://i0.wp.com/tutorial.eyehunts.com/wp-content/uploads/2022/12/Python-pretty-print-JSON-example.jpg?resize=491%2C460&ssl=1)
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.