Format and Validate JSON with CLI Tool in Python 3.14

Format and Validate JSON with CLI Tool in Python 3.14

Python has long included a built-in command-line helper for working with JSON through the json.tool module. It allowed developers to quickly pretty-print and verify JSON files without relying on external utilities. While functional, the invocation was slightly awkward and easy to forget.

Since Python 3.14, the json module itself can be executed directly from the command line. This provides a cleaner and more intuitive interface, which is now the preferred way to format and validate JSON. The older json.tool entry point remains available for compatibility but is considered deprecated.

Let's say we have JSON data stored in the test.json file:

echo '{"status":"success","data":[{"name":"John","age":25},{"name":"James","age":29}]}' > test.json

To format and confirm it is valid JSON, run:

python -m json test.json

The output of the command:

{
    "status": "success",
    "data": [
        {
            "name": "John",
            "age": 25
        },
        {
            "name": "James",
            "age": 29
        }
    ]
}

If the file contains invalid JSON, Python reports a parsing error with line and column details, making it easy to locate the problem.

For existing scripts, the previous command still works:

python -m json.tool test.json

However, for new workflows, python -m json is the recommended approach.

Leave a Comment

Cancel reply

Your email address will not be published.