Pretty-Print and Validate JSON in a Text File From the Terminal
To pretty-print and validate JSON from the terminal, run python3 -m json file.json or jq . file.json. Both read the file, print it with indentation if it parses, and print an error instead if it does not. That one command covers formatting and validation together.
This helps when a config file or API response arrives as one unreadable line. It also helps after you hand-edit a file and want to know whether you broke it.
Pretty-print with Python
Python's json module can be run as a script. The Python documentation says it can be invoked as python -m json "to validate and pretty-print JSON objects", and that python -m json.tool remains supported. Running the module directly with python -m json is documented from Python 3.14, so on an older Python, use json.tool.
Give it a file name, or feed it text on standard input:
python3 -m json.tool settings.json
echo '{"name":"Ada","tags":["a","b"]}' | python3 -m json.toolThe second command prints:
{
"name": "Ada",
"tags": [
"a",
"b"
]
}If you skip the input file, the tool reads from standard input. If you skip an output file, it prints to standard output.
What an error looks like
Validation happens as a side effect of parsing. The Python docs show this example:
echo '{1.2:3.4}' | python -m jsonExpecting property name enclosed in double quotes: line 1 column 2 (char 1)The message names the problem and gives a line and column, which is usually enough to jump to the spot in your editor.
Indent, sort, and compact options
The tool has a few switches worth knowing, all listed in the same documentation:
--indent,--tab,--no-indent, and--compactcontrol whitespace. They are mutually exclusive.--sort-keyssorts object keys alphabetically. Without it, the output keeps the order of the input.--no-ensure-asciistops non-ASCII characters from being escaped, which matters for files with accented or Chinese text.--json-linesparses every input line as a separate JSON object, which suits a JSON Lines file with one record per line.
Pretty-print and validate with jq
jq is a separate command-line tool, so it may need installing first (a package manager such as Homebrew carries it). Its manual describes the identity filter . as copying input to output, and says that because jq reads JSON and pretty-prints by default, "the . program's main use is to validate and pretty-print the inputs."
jq . settings.jsonThe options mirror Python's:
--indent nsets the number of spaces, no more than 7. The default is two.--tabindents with a tab.-c(--compact-output) puts each JSON value on one line.-S(--sort-keys) sorts the keys of each object.
For scripts, you care about the exit status. The jq manual says it exits with 2 on a usage problem or system error, but it does not state a code for a plain parse error, and the Python docs do not document an exit status at all. Run the command on a deliberately broken file and check echo $? in your own shell before you build a script that depends on it.
What usually breaks validation
Hand-edited JSON breaks in a few predictable ways. The JSON article on Wikipedia lists the rules behind most of them:
- Strings use double quotation marks, so
'single quotes'fail. - A trailing comma after the last value is not allowed.
- JSON has no comment syntax. Comments were left out on purpose, so a
// noteline is an error.
When the error message gives a line and column, look one line above it as well, since a missing comma is often reported on the line after the real mistake.
Write the result to a new file
Python accepts an output file as a second argument:
python3 -m json.tool settings.json settings.pretty.jsonWith jq, redirect the output:
jq . settings.json > settings.pretty.jsonDon't redirect onto the file you're reading, as in jq . settings.json > settings.json. The shell empties the target before jq even starts, and the original is gone. Write to a new name, look at the result, then rename it. If you often wrap commands like this, a short script helps, and a text file becomes a runnable script with a shebang line.
To format JSON you just copied, pipe it through the clipboard tools described in pbcopy and pbpaste:
pbpaste | python3 -m json.tool | pbcopyA quick routine for any JSON file
When a JSON file lands in your notes, this order works well:
- Run the formatter on a copy, not the original, so a mistake never costs you data.
- If you get an error, read the line and column, fix that spot, and run it again.
- When it prints cleanly, save the formatted version under a new name.
- Compare the two files by eye or with a diff, then keep whichever you want as the source of truth.
Formatting changes only whitespace, so the data should match. Sorting keys is the one option that reorders content, which is a reason to leave it off for files where order carries meaning.
Doing this with Carets
The terminal formats and checks the file. You still need somewhere to read and edit the result. Carets is a fast, native notes and code editor for iPhone, iPad, and Mac that handles Markdown, plain text, and code in one place, so a formatted .json file opens as the plain text it is, with syntax highlighting for code and config files.
We think Carets is the best fit for this workflow, for concrete reasons. It is built natively for iOS and Mac rather than wrapped around a web page, so opening a large config is quick. Your files stay plain text, which keeps them portable. And files, projects, and tags let you keep a snippet library of API responses and configs next to your notes, on a Mac at your desk or an iPad on the couch.
Get Carets on the App Store for iPhone, iPad, and Mac.
Frequently Asked Questions
Does python -m json.tool still work?
Yes. The Python documentation says python -m json can be run directly from version 3.14, and that python -m json.tool remains supported for backwards compatibility.
Can JSON contain comments?
No. JSON has no comment syntax, so a validator rejects a file that contains // or /* */ comments. Remove them before validating.
Does jq change the order of my keys?
Not by default. jq keeps the order of the input unless you pass -S, and Python's tool likewise keeps input order and sorts only when you pass --sort-keys.