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.

🌐
Python
docs.python.org › 3 › library › json.html
JSON encoder and decoder — Python 3.14.4 documentation
February 23, 2026 - If specified, separators should be an (item_separator, key_separator) tuple. The default is (', ', ': ') if indent is None and (',', ': ') otherwise. To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace.
Discussions

python - How to implement custom indentation when pretty-printing with the JSON module? - Stack Overflow
I hear it's possible to extend the json module: Is it possible to set it to only turn off indenting when inside the "layer3_1" object? If so, would somebody please tell me how? ... Your first code snippet is neither JSON nor Python. More on stackoverflow.com
🌐 stackoverflow.com
formatting json output in Python
json.dumps(obj, indent=2, sort_keys=False) More on reddit.com
🌐 r/learnpython
16
52
May 12, 2022
indented json file too big for terminal window pycharm
Why don't you write the result to a file? Why print to terminal? More on reddit.com
🌐 r/learnpython
4
3
September 3, 2024
JSON dumps indent tab
use simplejson instead More on reddit.com
🌐 r/learnpython
2
3
October 12, 2016
🌐
Vertabelo Academy
academy.vertabelo.com › course › python-json › writing-json-files › writing-to-json-file › jsondumps-options-the-indent
How to Read and Write JSON Files in Python | Learn Python | Vertabelo Academy
The indent parameter allows us to format the JSON array elements and object members in a more organized manner. A positive integer indent indent represents the number of spaces per level that should be used to indent the content.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-pretty-print-json
How to Pretty Print JSON in Python | DigitalOcean
September 16, 2025 - The best and easiest way to indent JSON output in Python is by using the the indent parameter in the json.dumps() function.
Top answer
1 of 16
32

(Note: The code in this answer only works with json.dumps() which returns a JSON formatted string, but not with json.dump() which writes directly to file-like objects. There's a modified version of it that works with both in my answer to the question Write two-dimensional list to JSON file.)

Updated

Below is a version of my original answer that has been revised several times. Unlike the original, which I posted only to show how to get the first idea in J.F.Sebastian's answer to work, and which like his, returned a non-indented string representation of the object. The latest updated version returns the Python object JSON formatted in isolation.

The keys of each coordinate dict will appear in sorted order, as per one of the OP's comments, but only if a sort_keys=True keyword argument is specified in the initial json.dumps() call driving the process, and it no longer changes the object's type to a string along the way. In other words, the actual type of the "wrapped" object is now maintained.

I think not understanding the original intent of my post resulted in number of folks downvoting it—so, primarily for that reason, I have "fixed" and improved my answer several times. The current version is a hybrid of my original answer coupled with some of the ideas @Erik Allik used in his answer, plus useful feedback from other users shown in the comments below this answer.

The following code appears to work unchanged in both Python 2.7.16 and 3.7.4.

from _ctypes import PyObj_FromPtr
import json
import re

class NoIndent(object):
    """ Value wrapper. """
    def __init__(self, value):
        self.value = value


class MyEncoder(json.JSONEncoder):
    FORMAT_SPEC = '@@{}@@'
    regex = re.compile(FORMAT_SPEC.format(r'(\d+)'))

    def __init__(self, **kwargs):
        # Save copy of any keyword argument values needed for use here.
        self.__sort_keys = kwargs.get('sort_keys', None)
        super(MyEncoder, self).__init__(**kwargs)

    def default(self, obj):
        return (self.FORMAT_SPEC.format(id(obj)) if isinstance(obj, NoIndent)
                else super(MyEncoder, self).default(obj))

    def encode(self, obj):
        format_spec = self.FORMAT_SPEC  # Local var to expedite access.
        json_repr = super(MyEncoder, self).encode(obj)  # Default JSON.

        # Replace any marked-up object ids in the JSON repr with the
        # value returned from the json.dumps() of the corresponding
        # wrapped Python object.
        for match in self.regex.finditer(json_repr):
            # see https://stackoverflow.com/a/15012814/355230
            id = int(match.group(1))
            no_indent = PyObj_FromPtr(id)
            json_obj_repr = json.dumps(no_indent.value, sort_keys=self.__sort_keys)

            # Replace the matched id string with json formatted representation
            # of the corresponding Python object.
            json_repr = json_repr.replace(
                            '"{}"'.format(format_spec.format(id)), json_obj_repr)

        return json_repr


if __name__ == '__main__':
    from string import ascii_lowercase as letters

    data_structure = {
        'layer1': {
            'layer2': {
                'layer3_1': NoIndent([{"x":1,"y":7}, {"x":0,"y":4}, {"x":5,"y":3},
                                      {"x":6,"y":9},
                                      {k: v for v, k in enumerate(letters)}]),
                'layer3_2': 'string',
                'layer3_3': NoIndent([{"x":2,"y":8,"z":3}, {"x":1,"y":5,"z":4},
                                      {"x":6,"y":9,"z":8}]),
                'layer3_4': NoIndent(list(range(20))),
            }
        }
    }

    print(json.dumps(data_structure, cls=MyEncoder, sort_keys=True, indent=2))

Output:

{
  "layer1": {
    "layer2": {
      "layer3_1": [{"x": 1, "y": 7}, {"x": 0, "y": 4}, {"x": 5, "y": 3}, {"x": 6, "y": 9}, {"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7, "i": 8, "j": 9, "k": 10, "l": 11, "m": 12, "n": 13, "o": 14, "p": 15, "q": 16, "r": 17, "s": 18, "t": 19, "u": 20, "v": 21, "w": 22, "x": 23, "y": 24, "z": 25}],
      "layer3_2": "string",
      "layer3_3": [{"x": 2, "y": 8, "z": 3}, {"x": 1, "y": 5, "z": 4}, {"x": 6, "y": 9, "z": 8}],
      "layer3_4": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
    }
  }
}
2 of 16
18

A bodge, but once you have the string from dumps(), you can perform a regular expression substitution on it, if you're sure of the format of its contents. Something along the lines of:

s = json.dumps(data_structure, indent=2)
s = re.sub('\s*{\s*"(.)": (\d+),\s*"(.)": (\d+)\s*}(,?)\s*', r'{"\1":\2,"\3":\4}\5', s)
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pretty-print-json
Python - Pretty Print JSON - GeeksforGeeks
July 23, 2025 - This code reads JSON data from ... 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....
Find elsewhere
🌐
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 - How to Pretty Print a JSON String in Python Pretty-printing JSON strings in Python is simple with the help of the built-in json module. By using the json.dumps() method with an indentation parameter …
🌐
GeeksforGeeks
geeksforgeeks.org › python › json-dumps-in-python
json.dumps() in Python - GeeksforGeeks
Example 2: This example formats JSON output to make it more readable using indentation. ... The JSON string becomes easy to read. Example 3: This example sorts dictionary keys alphabetically before converting them to JSON. ... Useful for consistent JSON output. Example 4: This example shows how json.dumps() converts a Python list into a JSON-formatted string, which is commonly used when sending list data through APIs.
Published   January 13, 2026
🌐
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.
🌐
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() ... a JSON string without any formatting, but we can use the indent parameter to specify the number of spaces to use for indentation....
🌐
PYnative
pynative.com › home › python › json › python prettyprint json data
Python PrettyPrint JSON Data
May 14, 2021 - The pprint module provides the capability to “pretty-print” any Python data structures. Now, let’s see how to use the pprint module to pretty-print JSON data. The pprint.pprint() function print the formatted representation of a JSON on the configured stream, followed by a newline · Construct a PrettyPrinter instance first by configuring indent and width values.
🌐
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

🌐
Sentry
sentry.io › sentry answers › python › write json data to a file in python
Write JSON data to a file in Python | Sentry
We can do this using Python’s built-in json library and file operations. Specifically, the json.dump function allows us to serialize a Python dictionary as JSON for writing to disk. For example: import json data = {"firstname": "John", "lastname": "Doe", "age": 35} with open("data.json", "w") as f: json.dump(data, f, indent=4)
🌐
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.
🌐
PythonHow
pythonhow.com › how › prettyprint-a-json-file
Here is how to prettyprint a JSON file in Python
By specifying the indent parameter in json.dumps(), you can control the number of spaces used for indentation. Setting indent to None or 0 will result in compact JSON without any extra whitespace. Note that if the JSON file is already loaded into a Python object, you can directly pass that object to json.dumps() instead of loading it from a file.
🌐
Newtum
apitest.newtum.com › examples › python-pretty-print-json
How to Pretty Print JSON in Python | `json.dumps` with Indent | API Navigator
import json data = { "user": {"name": "Eve", "id": 789}, "permissions": ["admin", "read", "write"], "active": True, "settings": {"theme": "dark", "notifications": None} } # Use indent=4 for 4-space indentation # Use sort_keys=True to sort the keys pretty_json = json.dumps(data, indent=4, sort_keys=True) print(pretty_json)CopyRun · When you get a JSON response from an API using the `requests` library, you can combine `response.json()` with `json.dumps()` to print it nicely. import requests import json response = requests.get('https://api.github.com/users/google') if response.status_code == 200: # response.json() parses the JSON into a Python dict # json.dumps() then formats that dict into a pretty string pretty_response = json.dumps(response.json(), indent=2) print(pretty_response) else: print(f"Request failed with status code {response.status_code}")CopyRun
🌐
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) ... Replace 'file.json' with the path to your JSON file. json.load(file) reads the JSON data from the file and parses it into a Python object (usually a dictionary or a list of dictionaries).
🌐
GeeksforGeeks
geeksforgeeks.org › python › pretty-print-json-in-python
Pretty Print JSON in Python - GeeksforGeeks
July 12, 2025 - Python3 · import json # Opening JSON file f = open('myfile.json',) # returns JSON object as # a dictionary data = json.load(f) print(json.dumps(data, indent = 1) # Closing file f.close() Output: { "emp1": { "name": "Lisa", "designation": "programmer", "age": "34", "salary": "54000" }, "emp2": { "name": "Elis", "designation": "Trainee", "age": "24", "salary": "40000" } } Comment ·