Working with REST API responses, configuration payloads, and log outputs in the Linux command line frequently involves minified, single-line JSON streams. While dedicated CLI processors like jq are widely celebrated, they are rarely pre-installed on minimal container images, lightweight virtual machines, or hardened production bastions. Fortunately, nearly every modern Linux distribution—including Ubuntu, Debian, RHEL, AlmaLinux, and Arch—includes a Python 3 runtime out of the box, complete with the standard library module json.tool for zero-dependency JSON formatting and schema validation.
Why Use python3 -m json.tool?
The json.tool module is part of Python’s core standard library. It requires no third-party package managers (like pip or apt) and no root administrative privileges. It reads JSON data from a file or standard input (pipe), validates that the syntax adheres to strict RFC 8259 formatting rules, and prints a cleanly indented, human-readable data tree with colourful syntax or sorted keys.
Basic Usage: Formatting a Minified JSON File
To pretty-print a compressed or unformatted JSON file directly to standard output:
python3 -m json.tool config.json
By default, json.tool indents elements with 4 spaces per nesting level. To save the beautifully formatted output into a new file:
python3 -m json.tool input.json > formatted.json
Formatting JSON Directly from a Terminal Pipe
When fetching data from web APIs or cloud metadata services with curl, pipe the raw response directly into json.tool:
curl -s https://api.github.com/users/octocat | python3 -m json.tool
This transforms the messy single-line response into a structured, readable hierarchy instantly.
Advanced Flags and Customisations in Modern Python 3
Python 3.8 and newer versions provide powerful command-line parameters to fine-tune how json.tool processes your data:
1. Customising Indentation Width
If you prefer a compact 2-space indentation style (common in web development and Docker files), use the --indent flag:
python3 -m json.tool --indent 2 payload.json
To produce compact, minified JSON without whitespace (useful for reducing HTTP payload size before sending API requests), pass --indent 0:
python3 -m json.tool --indent 0 formatted.json > minified.json
2. Sorting Dictionary Keys Alphabetically
When comparing differences between two configuration files using diff, disparate key orderings create false negatives. You can alphabetise all dictionary keys at every level using the --sort-keys argument:
python3 -m json.tool --sort-keys raw_data.json
3. In-Place File Reformatting
Rather than redirecting output to a temporary file, you can reformat and overwrite your source file in place by specifying the same filename for both input and output:
python3 -m json.tool settings.json settings.json
Using json.tool as an Automated Syntax Validator
In CI/CD deployment pipelines and bash pre-commit hooks, you can use json.tool to verify that JSON syntax is completely error-free:
if python3 -m json.tool config.json > /dev/null 2>&1; then
echo "JSON syntax is valid."
else
echo "ERROR: Invalid JSON detected!"
exit 1
fi
If the file contains broken brackets, trailing commas, or unescaped characters, json.tool exits with a non-zero exit status code and prints the exact line and column number where the syntax error occurred.