Python JSON dumps indent | Example

Python JSON method used indent parameter to specify the spaces that are used at the beginning of a line. If the indent parameter is not used By default it doesn’t use indentations and writes all data on a single line, which is not readable.

Example use JSON dumps indent in Python

Simple example code uses the indent parameter of json. dump() to specify the indentation value.

Pretty-Printed JSON data into a file with indent=4

import json

data = '[{"ID":101,"Name":"John","Class":"First"},' \

res = json.loads(data)

# Indent = 3
print(json.dumps(res, indent=4))


Python JSON dumps indent

How to prettyprint a JSON file?

Answer: The json module already implements some basic pretty printing with the indent parameter that specifies how many spaces to indent by:

import json

data = '["foo", {"bar":["baz", null, 1.0, 2]}]'
res = json.loads(data)

print(json.dumps(res, indent=4, sort_keys=True))


prettyprint a JSON file indent


Do comment if you have any doubts and suggestions on this Python JSON 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.