To pretty print JSON in Python, use the json.dumps() method with the indent parameter to format the output with readable indentation.

Using json.dumps() (Recommended for JSON strings)

import json

data = {"name": "Alice", "age": 30, "city": "New York", "hobbies": ["reading", "coding"]}
pretty_json = json.dumps(data, indent=2)
print(pretty_json)
  • indent=2 adds 2 spaces per indentation level (commonly used).

  • Use indent=4 for more spacing or indent='\t' for tab indentation.

  • Add sort_keys=True to alphabetically sort keys for consistent output.

  • Use ensure_ascii=False to preserve Unicode characters (e.g., "São Paulo", "日本").

Using json.dump() to Save to a File

with open('output.json', 'w') as f:
    json.dump(data, f, indent=2, sort_keys=True)

This writes a formatted, readable JSON file.

Using pprint (For Python objects, not JSON strings)

import pprint
pprint.pprint(data)
  • Useful for debugging Python dictionaries/lists.

  • Replaces double quotes with single quotes, so output is not valid JSON.

Best Practice: Use json.dumps(data, indent=2) for pretty printing JSON strings or saving formatted JSON files. Use pprint only for inspecting Python data structures.

Use the indent= parameter of json.dump() or json.dumps() to specify how many spaces to indent by:

>>> import json
>>> your_json = '["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> parsed = json.loads(your_json)
>>> print(json.dumps(parsed, indent=4))
[
    "foo",
    {
        "bar": [
            "baz",
            null,
            1.0,
            2
        ]
    }
]

To parse a file, use json.load():

with open('filename.txt', 'r') as handle:
    parsed = json.load(handle)
Answer from Blender on Stack Overflow
Top answer
1 of 15
3096

Use the indent= parameter of json.dump() or json.dumps() to specify how many spaces to indent by:

>>> import json
>>> your_json = '["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> parsed = json.loads(your_json)
>>> print(json.dumps(parsed, indent=4))
[
    "foo",
    {
        "bar": [
            "baz",
            null,
            1.0,
            2
        ]
    }
]

To parse a file, use json.load():

with open('filename.txt', 'r') as handle:
    parsed = json.load(handle)
2 of 15
500

You can do this on the command line:

python3 -m json.tool some.json

(as already mentioned in the commentaries to the question, thanks to @Kai Petzke for the python3 suggestion).

Actually python is not my favourite tool as far as json processing on the command line is concerned. For simple pretty printing is ok, but if you want to manipulate the json it can become overcomplicated. You'd soon need to write a separate script-file, you could end up with maps whose keys are u"some-key" (python unicode), which makes selecting fields more difficult and doesn't really go in the direction of pretty-printing.

You can also use jq:

jq . some.json

and you get colors as a bonus (and way easier extendability).

Addendum: There is some confusion in the comments about using jq to process large JSON files on the one hand, and having a very large jq program on the other. For pretty-printing a file consisting of a single large JSON entity, the practical limitation is RAM. For pretty-printing a 2GB file consisting of a single array of real-world data, the "maximum resident set size" required for pretty-printing was 5GB (whether using jq 1.5 or 1.6). Note also that jq can be used from within python after pip install jq.

🌐
ReqBin
reqbin.com › code › python › 0l6wsqxp › python-pretty-print-json-example
How do I pretty print JSON in Python?
The json.dumps() method takes a number of parameters, including the "indent" level for the JSON arrays, which will be used to pretty-print the JSON string. If the json.dumps() indentation parameter is negative 0, or an empty string, then there ...
Discussions

Pretty print JSON from URL
I've not done a lot in this space but something I have tried in the past with JSON's is to convert to a pandas DataFrame. I'm not sure if this is exactly what you are after, but the following stackoverflow post may help: https://stackoverflow.com/questions/21104592/json-to-pandas-dataframe More on reddit.com
🌐 r/learnpython
13
2
August 27, 2021
formatting json output in Python
json.dumps(obj, indent=2, sort_keys=False) More on reddit.com
🌐 r/learnpython
16
52
November 20, 2021
How to output preview of JSON file data structure?
Have you considered using pprint from pprint module? It eases the printing on the eyes. Then I would just do pprint(data) More on reddit.com
🌐 r/learnpython
7
13
November 9, 2021
how to have my view output pretty printed json
If you view the source code of the http response, it's pretty printed. If you want to see it the same way in the browser, you have to set the response mime type to application/json Edit: use content_type in the http response constructor https://docs.djangoproject.com/en/1.7/ref/request-response/#id3 More on reddit.com
🌐 r/django
7
5
October 10, 2014
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-pretty-print-json
How to Pretty Print JSON in Python | DigitalOcean
September 16, 2025 - Learn how to pretty print JSON in Python using built-in tools like json.dumps() and pprint to improve readability and debug structured data efficiently.
🌐
Reddit
reddit.com › r/learnpython › pretty print json from url
r/learnpython on Reddit: Pretty print JSON from URL
August 27, 2021 -

Hello Everyone,

I've hit a frustrating wall and after much googling I took a deep breath and felt the best approach was to ask you all for advice.

I'm making a very simple script and need assistance with what tool I should use to move forward.

Step 1: Fetch the URL Step 2: Make the JSON pretty. Such as a top-down listing of Title and URL only.

import requests

#make the URL call
page = requests.get('http://gleamlist.com:5000/api')
#verify the page has content - just for testing.
print(page.content)

I've tried using the json module but confuse myself and I've thought maybe BS4 would help but also confuse myself. I believe JSON is basically a dictionary and I want to parse out the info. Could anyone assist me with some tips or a specific topic to research and implement please?

🌐
PiShop Ltd
blog.pishop.co.za › home › blog 2 › how to pretty print a json file in python
How to Pretty Print a JSON File in Python - PiShop Blog
August 19, 2022 - It can pull this data in from a few sources and work with it like any other file. This is helpful when you want to pretty print a JSON file in Python. In this post, we show you how to pretty print a JSON file in two ways. Both will involve using the Terminal and command line, although you may ...
🌐
Make Tech Easier
maketecheasier.com › home › linux › how to pretty print a json file in python
How to Pretty Print a JSON File in Python - Make Tech Easier
June 24, 2022 - Also read: How to Utilize Python for Basic Linux System Administration and Networking Tasks · Below, we show you how to pretty print a JSON file in Python. For our examples, we use NeoVim and the built-in :term command.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › pretty-print-json-in-python
Pretty Print JSON in Python - GeeksforGeeks
July 12, 2025 - Whenever data is dumped into Dictionary ... the concept of Pretty Print Json comes into picture where we can display the JSON loaded into a presentable format....
🌐
W3Schools
w3schools.com › python › python_json.asp
Python JSON
JSON is text, written with JavaScript object notation. Python has a built-in package called json, which can be used to work with JSON data.
🌐
Jsontotable
jsontotable.org › blog › python › python-pretty-print-json
Python Pretty Print JSON - Format JSON with Indentation (2025) | JSON to Table Converter
January 16, 2025 - Quick Answer: Use json.dumps(data, indent=2) to pretty print JSON with 2-space indentation in Python.
🌐
W3Schools
w3schools.com › python › gloss_python_format_json.asp
Python Format JSON
json.dumps(x, indent=4, separators=(". ", " = ")) Try it Yourself » · Python JSON Tutorial JSON Parse JSON Convert into JSON Sort JSON
🌐
Sarthaks eConnect
sarthaks.com › 3474958 › how-do-i-pretty-print-json-data-in-python
How do I pretty-print JSON data in Python?
March 29, 2023 - Join Sarthaks live online classes for 7-12, CBSE, State Board, JEE & NEET courses led by experienced expert teachers. Learn, Practice Test, Analyse and ace your exam.
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › how to prettyprint a json file with python?
How to PrettyPrint a JSON File Using Python?
December 13, 2022 - Note: Learn more about working with Python strings by referring to our article on how to substring a string in Python. Use the json.tool to PrettyPrint JSON files directly in the terminal. Use the following command format: ... The JSON file contents print to the output with indentation level four.
🌐
Kodeclik
kodeclik.com › pretty-print-json-python
How to pretty print a JSON object in Python
October 27, 2025 - To pretty print JSON in Python use the json module and the dumps function in it with a specified level of indentation.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-pretty-print-json-in-python
How to Pretty Print JSON in Python
April 14, 2023 - This module provides a dumps() function that can serialize Python objects into a JSON formatted string. By default, this function produces a JSON string without any formatting, but we can use the indent parameter to specify the number of spaces to use for indentation. Here's an example of how to pretty print JSON in Python:
🌐
TutorialsPoint
tutorialspoint.com › pretty-print-json-in-python
Pretty Print JSON in Python
July 25, 2023 - It takes a single parameter, data, which is expected to be a JSON string. The purpose of this function is to parse the JSON data, format it in a human-readable way, and print the result. ... The json.loads function is used to parse the JSON string data and convert it into a Python object.
🌐
LornaJane
lornajane.net › posts › 2013 › pretty-printing-json-with-pythons-json-tool
Pretty-Printing JSON with Python’s JSON Tool | LornaJane
I’ve been using http://www.jsonprettyprint.net if I just needed something quick when playing around with different API results. ... Oh that looks useful, thanks for sharing! ... 7 year glitch! I just stumbled across this post when searching something like ‘lorna http tools’ to recap on your stimulatingly super presentation at the virtual Python North West Meetup yesterday!
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pretty-print-json
Python - Pretty Print JSON - GeeksforGeeks
July 23, 2025 - This code reads JSON data from ... module. The pprint module is used to pretty-print the JSON data with specific formatting options like an indentation of 2, a line width of 30 characters, and compact representation...
🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
August 20, 2025 - Free Bonus: Click here to download the free sample code that shows you how to work with JSON data in Python. When you pass in hello_frieda.json to json.tool, then you can pretty print the content of the JSON file in your terminal.