Python print JSON pretty | Example code

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:

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

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.