/commands/python/python-m-json-tool
python -m json.tool: pretty-print and validate JSON
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · Python 3.13.15updated 2026-10-20
- tool
- Python
- versions
- Python 3.5+, python -m json: Python 3.14+
- os
- any
python -m json.tool file.json pretty-prints a JSON file, and exits with status 1 and an error message when the input is not valid JSON. It reads standard input when no file is given, so it also works in pipes. Since Python 3.14 the same CLI is available as python -m json; json.tool keeps working.
Pretty-print from a pipe
echo '{"name": "errcodex", "tags": ["json", "cli"], "verified": true}' | python -m json.toolOutput
{
"name": "errcodex",
"tags": [
"json",
"cli"
],
"verified": true
}Validate a file
Invalid JSON prints the parser error with line and column, and the exit status is 1, which makes it usable in scripts and CI.
printf '{"a": 1,}\n' > bad.json
python -m json.tool bad.jsonOutput · exit 1
Illegal trailing comma before end of object: line 1 column 8 (char 7)
Sort keys, change indentation, compact output
echo '{"b": 2, "a": {"y": 1, "x": 2}}' > data.json
python -m json.tool --sort-keys --indent 2 data.json
python -m json.tool --compact data.jsonOutput
{
"a": {
"x": 2,
"y": 1
},
"b": 2
}
{"b":2,"a":{"y":1,"x":2}}Options
| Option | Effect | Since |
|---|---|---|
| infile | File to read (default: standard input) | — |
| outfile | File to write (default: standard output) | — |
| --sort-keys | Sort object keys alphabetically | 3.5 |
| --indent N | Indent with N spaces (default 4) | 3.9 |
| --tab | Indent with tabs | 3.9 |
| --no-indent | One line, with spaces after separators | 3.9 |
| --compact | One line, no extra whitespace | 3.9 |
| --json-lines | Treat every input line as a separate JSON document | 3.8 |
| --no-ensure-ascii | Keep non-ASCII characters instead of \uXXXX escapes | 3.9 |
Sources
Related pages
errors · pythonModuleNotFoundError: No module named '…'Python raises ModuleNotFoundError when the package is not installed for the interpreter that runs your code. Check sys.executable, then install with -m pip.cheat sheets · bashBash cheat sheet: variables, tests, loops and expansionsBash cheat sheet with the syntax you use daily: variables, parameter expansion, tests, loops, functions, arrays, redirection and safe script headers on a page.compat · pythonPython version support and end-of-life datesWhich Python versions are supported in October 2026: 3.14 and 3.15 get bugfixes, 3.11 to 3.13 get security fixes only, 3.10 reached end of life on 2026-10-01.