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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ json-dumps-in-python
json.dumps() in Python - GeeksforGeeks
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
๐ŸŒ
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.
Discussions

python - How to prettyprint a JSON file? - Stack Overflow
In JS tool prettier, it will not add 'line break' if the line width less than 80. I am looking for it. 2022-02-22T06:28:17.663Z+00:00 ... (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 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
`jsonable_encoder(obj)` vs `obj.model_dump(mode='json')`
I usually go with obj.model_dump_json() If I worried about performance I'd be using Go instead of Python More on reddit.com
๐ŸŒ r/FastAPI
4
4
October 2, 2024
json.dump doesn't write anything to the json file
Delete the dict.json you keep opening and finding empty, and then run your program. Did it get recreated? if not, it's probably writing the file to some other location the whole time. More on reddit.com
๐ŸŒ r/learnpython
18
10
November 28, 2022
How to retrieve JSON information through jinja
json.dumps(produce_list) does, in fact, turn the produce_list into a string. Just do return render_template("template.html", produce_list = produce_list) and you'll get what you are looking for. More on reddit.com
๐ŸŒ r/flask
2
5
May 16, 2017
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ json.html
JSON encoder and decoder โ€” Python 3.14.3 documentation
3 weeks ago - The json module can be invoked as a script via python -m json to validate and pretty-print JSON objects.
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ python โ€บ 0l6wsqxp โ€บ python-pretty-print-json-example
How do I pretty print JSON in Python?
Click Execute to run the Python Pretty Print JSON example online and see result. ... import json ugly_json = '[ {"Customer": 1, "name": "Alice", "country": ["Spain", "Madrid"]}, \ {"Customer": 2, "name": "Jack", "country": ["UK", "London"]} ]' parsed_json = json.loads(ugly_json) pretty_json = json.dumps(parsed_json, indent=2) print(pretty_json)
๐ŸŒ
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)
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.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-pretty-print-json
Python - Pretty Print JSON - GeeksforGeeks
July 23, 2025 - If the indent parameter of json.dumps() is negative, 0, or an empty string then there are no indentations and only newlines are inserted. By default, the indent is None and the data is represented in a single line. The code takes a JSON string containing student records, parses it into a Python data structure, then pretty...
Find elsewhere
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-json-pretty-print
Python JSON Pretty Print | Guide (With Examples)
February 1, 2024 - We then use the json.dumps() function with the indent parameter set to 4. This formats the JSON data with 4 spaces of indentation, making it easier to read. The result is then printed to the console.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-pretty-print-json-in-python
How to Pretty Print JSON in Python
April 14, 2023 - To pretty print JSON in Python, we can use the built-in json module. 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, ...
๐ŸŒ
PythonHow
pythonhow.com โ€บ how โ€บ prettyprint-a-json-file
Here is how to prettyprint a JSON file in Python
Here's an example that demonstrates how to pretty print a JSON file: 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 ...
๐ŸŒ
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) ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ pretty-print-json-in-python
Pretty Print JSON in Python - GeeksforGeeks
July 12, 2025 - # Write Python3 code here import ... dumping the # data decides to what level # spaces the user wants. print(json.dumps(json_object, indent = 1)) # Difference in the spaces # near the brackets can be seen print(json.dumps(js...
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ how to pretty print a json file in python?
How to Pretty Print a JSON file in Python? - Spark By {Examples}
May 31, 2024 - # Import JSON module import json # Read JSON data from file with open('file.json', 'r') as f: data = json.load(f) # Pretty-print JSON data and write back to file with open('file.json', 'w') as f: json.dump(data, f, indent=4) This will read the ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ json-dump-in-python
json.dump() in Python - GeeksforGeeks
June 20, 2024 - Example: ... import json # python object(dictionary) to be dumped dict1 ={ ('addresss', 'street'):'Brigade road', } # the json file where the output must be stored out_file = open("myfile.json", "w") json.dump(dict1, out_file, indent = 6) ...
๐ŸŒ
TestDriven.io
testdriven.io โ€บ tips โ€บ b5cd3100-2453-4941-9276-f71bdf8ec6e9
Tips and Tricks - Python - prettyprint JSON with json.dumps | TestDriven.io
import json users = [ {"username": "johndoe", "active": True}, {"username": "Mary", "active": False} ] print(json.dumps(users)) # [{"username": "johndoe", "active": true}, {"username": "Mary", "active": false}] print(json.dumps(users, indent=2)) ...
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ python โ€บ pbokf3iz โ€บ python-json-dumps-example
How to dump Python object to JSON using json.dumps()?
November 29, 2023 - indent - if specified then pretty-print JSON with that indent level ยท separators - separators to use when generating JSON output. To get a compact JSON use (',', ':'). sort_keys - if True, then dictionaries are sorted by key ยท To serialize a Python object to a JSON file, you can use the json.dump() method (without the "s") from the json module.
๐ŸŒ
JanBask Training
janbasktraining.com โ€บ community โ€บ python-python โ€บ how-to-prettyprint-a-json-file
How to prettyprint a JSON file? | JanBask Training Community
April 29, 2025 - import json data = {"name": "John", "age": 30, "city": "New York"} pretty_json = json.dumps(data, indent=2) print(pretty_json)
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ you must know python json dumps, but maybe not all aspects
You Must Know Python JSON Dumps, But Maybe Not All Aspects | Towards Data Science
January 20, 2025 - For example, we can let it become the PHP style as follows. json.dumps( my_dict, separators=('', ' => '), indent=2 ) JSON usually doesnโ€™t care about the order of the items. Therefore, when we dump a Python dictionary to a JSON string, the ...
๐ŸŒ
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 - For a complete guide, see our Python Write JSON to File tutorial. import json data = { "users": [ {"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"} ], "total": 2 } # Write pretty formatted JSON to file with open('users.json', 'w') as file: json.dump(data, file, indent=2) # users.json will ...
๐ŸŒ
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)