How to Pretty Print JSON in Python?

Estimated read time 1 min read

You can use the built-in json module in Python to pretty print JSON data. The json.dumps() function can be used to serialize Python objects to JSON format, and the indent parameter can be set to specify the number of spaces to use for indentation.

Here is an example:

import json

# A sample JSON string
json_str = '{"name": "John Smith", "age": 35, "city": "New York"}'

# Load the JSON string into a Python object
data = json.loads(json_str)

# Pretty print the object
print(json.dumps(data, indent=4))


    "name": "John Smith",
    "age": 35,
    "city": "New York"

In this example, we first load a JSON string into a Python object using json.loads(). Then, we use json.dumps() with the indent parameter set to 4 to pretty print the object.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply