Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Pretty Print JSON in Python

  • 17 Apr, 2025
  • Com 0
Pretty-Print JSON in Python

There are often times when the JSON data you are getting is hard to read and cluttered. We need to format it to make it human-readable and get meaningful insights.

Pretty-printing JSON means turning a compact, often hard-to-read JSON string into a readable format —an indented, human-friendly representation.

Using the json Module

The most efficient and easiest way to pretty-print JSON in Python is using the json module’s .dumps() method with the “indent” argument.

Set the indent parameter for formatting. You can set the 2, 4, or 6 as per your requirements.

Printing pretty json in Python using indent argument

import json

# Sample data: list of user information dictionaries
data = [
 {"name": "David", "age": 30, "city": "New York"},
 {"name": "Anna", "age": 25, "city": "Los Angeles"},
 {"name": "Mike", "age": 28, "city": "Chicago"}
]

print("Before Pretty print: ", data)

# Convert data to a formatted JSON string
pretty_json = json.dumps(data, indent=2)

print("After Pretty print: ", pretty_json)

Output of pretty print json

We passed indent=2, which means it formats the JSON data based on 2 spaces, making it human-readable and analyzable.

You can also sort the keys and preserve unicode characters by setting the sort_keys and ensure_ascii arguments.

import json

# Sample data: list of user information dictionaries
data = [
  {"name": "David", "age": 30, "city": "New York"},
  {"name": "Anna", "age": 25, "city": "Los Angeles"},
  {"name": "Mike", "age": 28, "city": "Chicago"}
]

pretty_print = json.dumps(data, indent=2, sort_keys=True, ensure_ascii=False)

print(pretty_print)

Output of sort the keys and preserve unicode characters

json.dump() to file

If you have JSON data and want to write it in an indented and proper format to a file, you can do so by using the json.dump() method with an indent argument.

import json

data = {
    "users": [
        {"id": 101, "name": "Krunal", "active": True},
        {"id": 201, "name": "Ankit",   "active": False}
    ],
    "count": 2
}

with open("output_pretty.json", "w", encoding="utf-8") as fp:
    json.dump(
        data,
        fp,
        indent=4,            # 4 spaces per level
        sort_keys=True,      # alphabetize the keys
        ensure_ascii=False,  # allow non‑ASCII characters unescaped
        separators=(',', ': ')  # tweak spacing around separators
    )

Here is the written output_pretty.json file:

Output formatted json file

Command-Line Pretty Printing

There is a special module that you can use to pretty-print json in the command line or terminal. That module is “json.tool”. You can directly print the pretty-print json to the terminal from any file.

Go to the terminal and type the command below:

python -m json.tool output_pretty.json

Since we already have the output_pretty.json file, we can see the following output in the terminal.

Pretty-print json output on terminal using Python

Third-Party Libraries

For faster alternatives, you can use the ujson or simplejson library.

You need to install the ujson library to use its .dumps() method.

pip install ujson

Now, you can use its method.

import ujson

data = {
    "users": [
        {"id": 101, "name": "Krunal", "active": True},
        {"id": 201, "name": "Ankit",   "active": False}
    ],
    "count": 2
}

pretty_json = ujson.dumps(data, indent=4)

print(pretty_json)

Output of ujson module

pprint module

The pprint() function from the pprint module prints your JSON data in a more readable format and can sort the keys alphabetically. Make sure to install the pprint module first and then use its methods.

Using pprint module

 

import json
from pprint import pprint

data = [
  {"name": "David", "age": 30, "city": "New York"},
  {"name": "Anna", "age": 25, "city": "Los Angeles"},
  {"name": "Mike", "age": 28, "city": "Chicago"}
]

print("Before Pretty print: ", data)

# Pretty print JSON using pprint
print("After Pretty print: ")
pprint(data, indent=2, sort_dicts=False)

# Output:
# Before Pretty print:  [{'name': 'David', 'age': 30, 'city': 'New York'}, {'name': 'Anna', 'age': 25, 'city': 'Los Angeles'}, {'name': 'Mike', 'age': 28, 'city': 'Chicago'}]
# After Pretty print:
# [ {'name': 'David', 'age': 30, 'city': 'New York'},
#   {'name': 'Anna', 'age': 25, 'city': 'Los Angeles'},
#   {'name': 'Mike', 'age': 28, 'city': 'Chicago'}]

That’s all!

Post Views: 22
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Handle JSON NULL in Python
How to a Read Binary File in Different Ways in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend