Results.

SCEPTRA.TECH

Check a JSON File Locally With Python, Without Overwriting the Original

Local JSON inspection preserves the original while checking parser and schema limits

A JSON error does not require pasting the whole file into an online formatter. If Python is already installed, its standard library can parse a local file and print a readable version in your terminal. I would use that as a first inspection step, while keeping one distinction clear: Python accepting the file is not the same as the receiving application accepting its contents.

Inspect a file without replacing it

Start with a small, known file in a local folder. For example, save this invented configuration as example.json. It contains no real account details:

{"project":"demo","enabled":true,"retries":3}

In a terminal opened in that folder, run:

python -m json.tool example.json

Use the command that starts your installed Python 3 interpreter. On many macOS or Linux setups that is python3, so the command becomes python3 -m json.tool example.json. On Windows, py -m json.tool example.json is an option when the Python launcher is installed. Check your installation rather than assuming all three names exist.

With only an input filename, the formatted result goes to standard output, normally your terminal. This command does not ask the tool to replace the input file. Python 3.14 added the shorter python -m json form; the json.tool form remains supported for compatibility.

Use the error location as a starting point

If parsing fails, read the message and its line and column. Look around that position for a missing comma, a single-quoted property name or an unfinished string. A parser may notice the problem after the character that caused it, so inspect the preceding value too.

Ordinary JSON property names use double quotes. Boolean values are true and false, and the empty value is null, not Python's differently spelled equivalents. A file intended for a JavaScript-like configuration format with comments or other extensions may need that format's own parser instead. Do not remove material blindly just to make a generic JSON check pass.

Fix one issue in your editor, save and rerun. If your filename contains spaces, quote the path for your shell. Keep real credentials out of screenshots and shared terminal transcripts: running a formatter locally does not make its displayed output safe to share.

Write a separate review copy, if needed

Python's command-line interface accepts a second filename for output. Choose a new name that does not already contain anything important:

python -m json.tool example.json example.pretty.json

Inspect the new file before using it. Do not use the same path for input and output, and do not redirect output over the original file. Keeping the original makes it easier to review what the parser and serializer changed.

Know the two easy-to-miss limits

Python documents that its default JSON decoder accepts repeated names in an object and keeps only the last value. A configuration with the same option twice can therefore lose a value when reformatted. RFC 8259 says object names should be unique and describes inconsistent behavior between implementations. Treat duplicate names as a problem to resolve in the source, not something a formatter has safely repaired.

Python also accepts NaN, Infinity and -Infinity by default, even though those number literals are outside the JSON specification. Successful output from this command is therefore not proof of strict interoperability.

Finally, parsing checks structure, not your application's rules. It cannot tell you whether a required field is missing, a setting has the wrong meaning or an endpoint expects a different data shape. Use the receiving application's documented validation or schema checks afterward. Python also warns that hostile JSON can consume substantial CPU and memory, so avoid feeding arbitrary huge files into a casual check.

Sources

Comments