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.
🌐
Python
docs.python.org › 3 › library › json.html
JSON encoder and decoder — Python 3.14.3 documentation
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)
🌐
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, ...
🌐
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...
🌐
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.
Find elsewhere
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.

🌐
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 ...
🌐
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 ...
🌐
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)
🌐
ReqBin
reqbin.com › code › python › pbokf3iz › python-json-dumps-example
How to dump Python object to JSON using json.dumps()?
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.
🌐
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) ...
🌐
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 ...
🌐
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)) ...
🌐
Rayobyte
rayobyte.com › blog › how-to-use-json-dumps-in-python
How to Store Scraped Web Data in Python Using JSON Dumps
March 26, 2025 - Metadata inclusion: Sometimes, when ... of the Python dictionary. In those cases, you’ll have to manually insert this information into the dictionary before calling ‘json.dump’. Custom serialization: In some advanced use cases, you might need to implement custom serialization logic by subclassing ‘json.JSONEncoder’. This allows for fine-grained control over how objects are serialized to JSON. The ‘indent’ parameter can be used to pretty-print the ...
🌐
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)