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
3098

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.

๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-pretty-print-json
How to Pretty Print JSON in Python | DigitalOcean
September 16, 2025 - Reconstruct your custom Python objects from a JSON string by using the object_hook parameter in json.loads() to intercept and transform the data. Improve the debugging experience by using the rich library to pretty-print JSON with syntax highlighting directly in your terminal.
Discussions

formatting json output in Python
json.dumps(obj, indent=2, sort_keys=False) More on reddit.com
๐ŸŒ r/learnpython
16
52
May 12, 2022
pretty-print json in python (pythonic way) - Stack Overflow
I know that the pprint python standard library is for pretty-printing python data types. However, I'm always retrieving json data, and I'm wondering if there is any easy and fast way to pretty-prin... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Explain Python Pretty Print JSON
Learn the art of beautifying and organizing your JSON data effortlessly with this step-by-step guide on how to use Python Pretty Print JSON. More on accuweb.cloud
๐ŸŒ accuweb.cloud
1
December 6, 2023
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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-pretty-print-json
Python - Pretty Print JSON - GeeksforGeeks
July 23, 2025 - The code takes a JSON string containing student records, parses it into a Python data structure, then pretty-prints the JSON data with proper indentation for improved readability.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-pretty-print-json-in-python
How to Pretty Print JSON in Python
April 14, 2023 - 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:
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ python โ€บ 0l6wsqxp โ€บ python-pretty-print-json-example
How do I pretty print JSON in Python?
To pretty print a JSON string in Python, you can use the json.dumps(indent) method of the built-in package named json. First, you need to use the json.loads() method to convert the JSON string into a Python object.
๐ŸŒ
Medium
medium.com โ€บ @blogshub4 โ€บ how-to-pretty-print-a-json-string-in-python-98a85f99ecb4
How to Pretty Print a JSON String in Python | by Blogshub | Medium
December 22, 2024 - Hereโ€™s an example of pretty-printing a minified JSON string: ... # Minified JSON string json_data = '{"name": "Dharmender", "age": 25, "city": "Bangalore"}'# Convert to Python object parsed_data = json.loads(json_data)# Pretty print with indentation pretty_json = json.dumps(parsed_data, indent=4) print(pretty_json)
๐ŸŒ
Better Stack
betterstack.com โ€บ community โ€บ questions โ€บ how-to-print-json-file-in-python
How to Prettyprint a Json File in Python? | Better Stack Community
import json # Load JSON data from a file with open('file.json', 'r') as file: json_data = json.load(file) # Pretty print the JSON data pretty_json = json.dumps(json_data, indent=4) print(pretty_json) ...
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ formatting json output in python
r/learnpython on Reddit: formatting json output in Python
May 12, 2022 -

Hi,

I would like to read json into Python code, and then output processed json. In order to get started with this, I have written very basic Python, and am attempting to read in very basic json I found online.

The input json is:

{
    "firstName": "John",
    "lastName": "Doe",
    "hobbies": ["biking", "coding", "rapping"],
    "age": 35,
    "children": [
        {
            "firstName": "hector",
            "age": 6
        },
        {
            "firstName": "cassandra",
            "age": 8
        }
    ]
}

The code is:

import json

if __name__ == '__main__':
    
    print( "start" )

    # read and load input json
    json_input_filename = "input.json"
    json_input = open( json_input_filename )

    json_input_dict = json.load( json_input )

    # write output json
    json_output_filename = "output.json"
    with open( json_output_filename, 'w' ) as json_output:
        json.dump( json_string, json_output )
  

    print( f"end" )

and the output is:

"{\"firstName\": \"John\", \"lastName\": \"Doe\", \"hobbies\": [\"biking\", \"coding\", \"rapping\"], \"age\": 35, \"children\": [{\"firstName\": \"hector\", \"age\": 6}, {\"firstName\": \"cassandra\", \"age\": 8}]}"

What can I do in order to preserve something resembling the original formatting? I'm going to load this output into some other code in order to process it further.

Thank you very much

๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ json.html
JSON encoder and decoder โ€” Python 3.14.3 documentation
February 23, 2026 - The json module can be invoked as a script via python -m json to validate and pretty-print JSON objects.
๐ŸŒ
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....
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ pprint.html
pprint โ€” Data pretty printer
Return the formatted representation of object as a string. indent, width, depth, compact, sort_dicts and underscore_numbers are passed to the PrettyPrinter constructor as formatting parameters and their meanings are as described in the documentation above.
๐ŸŒ
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 - Use the pprint library to pretty print JSON files as strings. For example: import json import pprint my_file = open("my_file.json") loaded_json = json.load(my_file) pprint.pprint(loaded_json) The pprint method replaces double quotes with single ...
๐ŸŒ
GitHub
github.com โ€บ clarketm โ€บ pprintjson
GitHub - clarketm/pprintjson: A json pretty printer for Python ๐Ÿ
$ pprintjson -c '{ "a": 1, "b": "string", "c": true }' Pretty print JSON from a string with an indent of 1.
Author ย  clarketm
Top answer
1 of 1
1
Explain Python Pretty Print JSONJSON (JavaScript Object Notation) is a widely used data format for data interchange. It's human-readable and machine-friendly, making it a popular choice for configuration files, APIs, and data storage. Sometimes, when working with JSON data in Python, you might encounter large or complex JSON structures that are challenging to read. That's where Python's "pretty print" functionality comes into play.Pretty printing is the process of formatting JSON data to make it more legible and visually appealing to humans. Python provides a built-in module called `json` that includes a `dumps()` method. Using this method with specific parameters lets you easily print JSON data.In this article, we'll explore using Python's `json.dumps()` method to pretty print JSON data.PrerequisitesBefore we dive into pretty printing JSON in Python, ensure you have Python installed on your system. You can download it from the official Python website: https://www.python.org/downloads/What is JSON?JSON is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. It's often used to transmit data between a server and a web application or between different parts of an application.JSON data is represented as a collection of key-value pairs, similar to Python dictionaries. Here's a simple example:json{    "name": "John Doe",    "age": 30,    "city": "New York"}In Python, JSON data is typically converted to dictionaries or lists using the `json` module, making it easy to work with.Using `json.dumps()` for Pretty PrintingPython's `json.dumps()` function converts a Python object into a JSON formatted string. By default, the JSON output is compact and not very human-readable. You can use the `indent` parameter to make it more readable.Here's the basic syntax of `json.dumps()` with the `indent` parameter:import jsonpretty_json = json.dumps(your_data, indent=4) `your_data`: This is the Python object (e.g., dictionary or list) that you want to convert to JSON. `indent=4`: This parameter specifies the number of spaces to use for indentation in the resulting JSON string. In this case, we use 4 spaces to make it nicely formatted.Example: Pretty Printing JSONLet's see an example of pretty printing JSON in Python:import json# Sample JSON datadata = {    "name": "John Doe",    "age": 30,    "city": "New York",    "skills": }# Pretty print the JSON datapretty_json = json.dumps(data, indent=4)# Print the pretty JSONprint(pretty_json)When you run this code, it will produce the following nicely formatted JSON output:json{    "name": "John Doe",    "age": 30,    "city": "New York",    "skills": }As you can see, the JSON data is now structured with proper indentation, making it much easier to read and understand.Use Cases for Pretty PrintingPretty printing JSON is particularly helpful in scenarios where: Debugging: When you're working with JSON data and need to debug or inspect it, pretty printing makes it more human-readable and helps you identify issues more easily. Logging: If you're logging JSON data in your application, pretty print formatting can make your log files more organized and user-friendly. Configuration Files: Pretty printing is beneficial when dealing with configuration files in JSON format. It ensures that configuration settings are neatly organized. API Development: When building or consuming JSON APIs for data exchange, pretty printing can assist in documenting and testing the API endpoints.ConclusionPython's `json.dumps()` method with the `indent` parameter is a valuable tool for pretty much printing JSON data. It allows you to format JSON in a human-readable way, making it easier to work with, debug, and understand. Whether you're developing web applications, working with configuration files, or dealing with API data, pretty printing JSON can significantly improve your workflow and code readability.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-json-pretty-print
Python JSON Pretty Print | Guide (With Examples)
February 1, 2024 - In this updated code, we use the isoformat() method to convert the datetime object into an ISO formatted string. Now, when we pass the dictionary to json.dumps(), it successfully pretty prints the JSON data. Knowing how to troubleshoot and work around common issues like this is a crucial part of mastering Pythonโ€™s JSON module.
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-pretty-print-json
How To Pretty Print JSON In Python
August 12, 2025 - Learn 4 easy methods to pretty print JSON in Python with real examples. Master json.dumps(), pprint, and more formatting techniques for readable JSON output.
๐ŸŒ
Codingem
codingem.com โ€บ home โ€บ python pretty print json
Python Pretty Print JSON [with 3 Examples] - codingem.com
January 23, 2023 - Python pretty-printed JSON has indentations, spacings, and separators for your convenience. To pretty-print, call json.dumps(indent,separator)
๐ŸŒ
PythonHow
pythonhow.com โ€บ how โ€บ prettyprint-a-json-file
Here is how to prettyprint a JSON file in Python
import json # Load the JSON data from a file with open('your_file.json') as file: data = json.load(file) # Pretty print the JSON data pretty_json = json.dumps(data, indent=4) # Print or save the pretty printed JSON print(pretty_json) ... In this example, you need to replace 'your_file.json' ...
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ json โ€บ python prettyprint json data
Python PrettyPrint JSON Data
May 14, 2021 - Python Write Indented and Pretty-printed JSON into a file. Prettyprint JSON file and JSON string. Use pprint module to pretty-print JSON. Pretty-print JSON from the command line