With Python 2.6+ or 3 you can use the json.tool module:

echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool

or, if the JSON is in a file, you can do:

python -m json.tool my_json.json

if the JSON is from an internet source such as an API, you can use

curl http://my_url/ | python -m json.tool

For convenience in all of these cases you can make an alias:

alias prettyjson='python -m json.tool'

For even more convenience with a bit more typing to get it ready:

prettyjson_s() {
    echo "$1" | python -m json.tool
}

prettyjson_f() {
    python -m json.tool "$1"
}

prettyjson_w() {
    curl "$1" | python -m json.tool
}

for all the above cases. You can put this in .bashrc and it will be available every time in shell. Invoke it like prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'.

Note that as @pnd pointed out in the comments below, in Python 3.5+ the JSON object is no longer sorted by default. To sort, add the --sort-keys flag to the end. I.e. ... | python -m json.tool --sort-keys.

Another useful option might be --no-ensure-ascii which disables escaping of non-ASCII characters (new in version 3.9).

Top answer
1 of 16
5228

With Python 2.6+ or 3 you can use the json.tool module:

echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool

or, if the JSON is in a file, you can do:

python -m json.tool my_json.json

if the JSON is from an internet source such as an API, you can use

curl http://my_url/ | python -m json.tool

For convenience in all of these cases you can make an alias:

alias prettyjson='python -m json.tool'

For even more convenience with a bit more typing to get it ready:

prettyjson_s() {
    echo "$1" | python -m json.tool
}

prettyjson_f() {
    python -m json.tool "$1"
}

prettyjson_w() {
    curl "$1" | python -m json.tool
}

for all the above cases. You can put this in .bashrc and it will be available every time in shell. Invoke it like prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'.

Note that as @pnd pointed out in the comments below, in Python 3.5+ the JSON object is no longer sorted by default. To sort, add the --sort-keys flag to the end. I.e. ... | python -m json.tool --sort-keys.

Another useful option might be --no-ensure-ascii which disables escaping of non-ASCII characters (new in version 3.9).

2 of 16
1654

You can use: jq

It's very simple to use and it works great! It can handle very large JSON structures, including streams. You can find their tutorials here.

Usage examples:

$ jq --color-output . file1.json file1.json | less -R

$ command_with_json_output | jq .

$ jq # stdin/"interactive" mode, just enter some JSON

$ jq <<< '{ "foo": "lorem", "bar": "ipsum" }'
{
  "bar": "ipsum",
  "foo": "lorem"
}

Or use jq with identity filter:

$ jq '.foo' <<< '{ "foo": "lorem", "bar": "ipsum" }'
"lorem"
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-pretty-print-json
How to Pretty Print JSON in Python | DigitalOcean
September 16, 2025 - There are several tools to automatically pretty-print JSON in Python scripts. Here are a few options: You can use json.tool in the terminal to pretty-print JSON from a file or standard input: ... You will have to first install jq using the pip install jq command.
🌐
Skorks
skorks.com › 2013 › 04 › the-best-way-to-pretty-print-json-on-the-command-line
The Best Way To Pretty Print JSON On The Command-Line - Skorks
I love the command-line and whenever ... way to pretty print my JSON on the command-line. It should be simple, quick, easy to remember, easy to install – we’re not trying to solve complex algorithms, just format some JSON. One way that is always available is the Python JSON ...
🌐
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.
🌐
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. The json.tool submodule implements this interface. If the optional infile and outfile arguments are not specified, sys.stdin and sys.stdout ...
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.

🌐
DEV Community
dev.to › anuragrana › pretty-print-json-using-the-command-line-tools-1bpk
Pretty print JSON using the command line tools - DEV Community
July 11, 2019 - Option 1. Using python cat FileName.json | python -m json.tool · Option 2. Colors and multiple options. cat FileName.json~ | jq '' Option 3. Pretty print with syntax highlighting.
Find elsewhere
🌐
Akshayranganath
akshayranganath.github.io › pretty-print-json-with-command-line
Pretty Print JSON & Move it to Command Line – Akshay Ranganath's Blogs
June 16, 2017 - Once done, you can execute this as a built-in command from any folder. After it is setup correctly, you can execute it like this. $ pretty_printer.py --file rules.json Pretty printer complete.
🌐
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() function that can serialize Python objects into a JSON formatted string. 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:
🌐
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 - If you set indent=0 or pass an empty string, the output will still be on a single line but with newlines inserted. 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)
🌐
Medium
medium.com › @digestize › how-to-pretty-print-json-in-python-command-line-8b9fe90c4a52
How to pretty print JSON in python command line? | by AI/Data Science ...
June 28, 2020 - How to pretty print JSON in python command line? Here’s the command line to do so: python -m json.tool Handling errors: You may see the following error message: Expecting property name …
🌐
Will McGugan
willmcgugan.com › blog › tech › post › pretty-printing-json-with-rich
Pretty printing JSON with Rich
You can also pretty print JSON files from the command line with the following: python -m rich.json data.json · Here's an example of the output: Pretty printing a JSON file from the command line · This is admittedly a small addition to Rich ...
🌐
HashBangCode
hashbangcode.com › article › pretty-print-json-python
Pretty Print JSON With Python | #! code
Note that if you are using Python 3 then the commands here will use the command python3 instead. You can also use this technique for testing scripts or curl requests, in fact anything that produces JSON output can be piped into json.tool and formatted correctly. Here is an example of piping a PHP script output into the json.tool.
🌐
Jontsai
jontsai.com › 2017 › 10 › 09 › how-to-pretty-print-json-from-cli
How to pretty print JSON from CLI - Jonathan Tsai
There are two easy ways of pretty-printing JSON from command-line to aid in visual inspection. The first requires no installation, and the second requires a minimal installation but also provides syntax highlighting and manipulation capabilities. Optional: Add alias to .bashrc alias json='python -mjson.tool'
🌐
thanoskoutr
thanoskoutr.com › posts › pretty-print-json
Pretty Print JSON in Linux Terminal :: thanoskoutr
February 16, 2023 - If jq is not installed on your system, you can usually get it via your package manager: # On Debian-based systems sudo apt-get install jq # On Red Hat-based systems sudo yum install jq · jq is a powerful command-line JSON processor. It’s a versatile tool that can be used not only for ...
🌐
Make Tech Easier
maketecheasier.com › home › linux › how to pretty print a json file in python
How to Pretty Print a JSON File in Python - Make Tech Easier
June 24, 2022 - Below, we show you how to pretty print a JSON file in Python. For our examples, we use NeoVim and the built-in :term command.
🌐
Juha-Matti Santala
hamatti.org › posts › pretty-print-and-validate-json-on-command-line-with-json-tool
Pretty print and validate JSON on command line with json.tool : Juha-Matti Santala
August 25, 2024 - Python is not only a great programming language but its built-in command line tools make it a nice companion on terminal as well. Today’s small but powerful example is using json.tool from json module as a command line tool to pretty print and validate JSON.
🌐
Nick Janetakis
nickjanetakis.com › blog › pretty-print-json-in-your-terminal-with-jq-or-python
Pretty Print JSON in Your Terminal with jq or Python — Nick Janetakis
July 4, 2023 - # Pretty print the contents of your clipboard with 2 spaces of indentation ppjson # Pretty print the content of a file ppjson data.json · In either case you can pass in any arguments that jq supports after an optional file name. That lets you parse the JSON such as ppjson .glossary.title -r to return back “example glossary” in the above example JSON. The video below demos running these commands.
🌐
Studytonight
studytonight.com › python-howtos › how-to-pretty-print-a-json-file-in-python
How to Pretty Print a JSON file in Python - Studytonight
In this article, we learned how to pretty print a JSON file using json.dumps() function. We can even increase the value of the indent keyword to see changes in the output. We also discussed a single command that can be used on the Python command ...