๐ŸŒ
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)
๐ŸŒ
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) ...
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
Explain Python Pretty Print JSON
API Development: When building or consuming JSON APIs for data exchange, pretty printing can assist in documenting and testing the API endpoints. Pythonโ€™s `json.dumps()` method with the `indent` parameter is a valuable tool for pretty much printing JSON data. More on accuweb.cloud
๐ŸŒ accuweb.cloud
1
December 6, 2023
formatting json output in Python
json.dumps(obj, indent=2, sort_keys=False) More on reddit.com
๐ŸŒ r/learnpython
16
52
May 12, 2022
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
๐ŸŒ
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)
๐ŸŒ
DEV Community
dev.to โ€บ pineapple_26 โ€บ json-pretty-print-using-python-with-examples-12ha
JSON Pretty Print Using Python - With Examples - DEV Community
November 20, 2025 - Write formatted JSON to file with open('output.json', 'w') as file: json.dump(data, file, indent=4, sort_keys=True) indent Specifies the number of spaces for indentation. Common values are 2 or 4. sort_keys Sorts dictionary keys alphabetically for consistent output. ensure_ascii By default, non-ASCII characters are escaped. Set to False to preserve them. Pretty printing JSON in Python is straightforward with the built-in json module.
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_json.asp
Python JSON
The json.dumps() method has parameters to make it easier to read the result: Use the indent parameter to define the numbers of indents: ... You can also define the separators, default value is (", ", ": "), which means using a comma and a space ...
๐ŸŒ
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 - Pretty Print JSON with Indentation: Use the json.dumps() method to convert the Python object back into a JSON string, and specify the indent parameter to define the level of indentation for readability.
๐ŸŒ
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 ...
Find elsewhere
๐ŸŒ
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 - Python's built-in json module makes pretty printing easy with the indent parameter. You can also use our JSON Formatter tool for quick formatting. Quick Answer: Use json.dumps(data, indent=2) to pretty print JSON with 2-space indentation in Python.
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ json-pretty-print
Best JSON Pretty Print Online
JSON Pretty Print helps Pretty JSON data and Print JSON data. It's very simple and easy way to prettify JSON and pretty print 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.

๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-pretty-print-json
How to Pretty Print JSON in Python | DigitalOcean
September 16, 2025 - Reconstruct your custom Python ... library to pretty-print JSON with syntax highlighting directly in your terminal. We can use the dumps() method to get the pretty formatted JSON string....
๐ŸŒ
Real Python
realpython.com โ€บ python-json
Working With JSON Data in Python โ€“ Real Python
August 20, 2025 - You write JSON with Python using json.dump() to serialize data to a file. You can minify and prettify JSON using Pythonโ€™s json.tool module.
๐ŸŒ
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-prints the JSON data with proper indentation for improved readability.
๐ŸŒ
MangoHost
mangohost.net โ€บ mangohost blog โ€บ python pretty print json โ€“ format for readability
Python Pretty Print JSON โ€“ Format for Readability
August 3, 2025 - import json # Reading and reformatting existing JSON files def prettify_json_file(input_file, output_file=None): with open(input_file, 'r') as f: data = json.load(f) pretty_json = json.dumps(data, indent=4, sort_keys=True) if output_file: with open(output_file, 'w') as f: f.write(pretty_json) else: print(pretty_json) # Usage prettify_json_file('config.json', 'config_formatted.json') Beyond basic indentation, Python offers extensive formatting control:
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ json.html
json โ€” JSON encoder and decoder
3 weeks ago - The json module can be invoked as a script via python -m json to validate and pretty-print JSON objects.
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.
๐ŸŒ
GitHub
github.com โ€บ nlohmann โ€บ json
GitHub - nlohmann/json: JSON for Modern C++ ยท GitHub
Yuanhao Jia fixed the GDB pretty printer. Fallen_Breath fixed an example for JSON Pointer.
Starred by 49.1K users
Forked by 7.3K users
Languages ย  C++ 96.9% | CMake 2.0% | Python 0.6% | Makefile 0.3% | Starlark 0.1% | Jinja 0.1%
๐ŸŒ
Kodeclik
kodeclik.com โ€บ pretty-print-json-python
How to pretty print a JSON object in Python
October 27, 2025 - To pretty print JSON using this module, you first import the json module and then use the json.dumps() function with the indent parameter to specify the level of indentation required.