LinuxCommandLibrary

python-m-json.tool

Format JSON data

TLDR

Pretty-print JSON from a file

$ python -m json.tool [path/to/file.json]
copy

Validate and pretty-print JSON from standard input
$ echo '[{"key": "value"]}' | python -m json.tool
copy

SYNOPSIS


python -m json.tool
[<options>] [<input_file>]
or
command_output | python -m json.tool [<options>]

PARAMETERS

-h, --help
    Displays a help message and exits, showing available options and usage.

--sort-keys
    Sorts the output of dictionaries by key alphabetically, making the output more deterministic and readable.

--json-lines
    Parses the input as a sequence of JSON objects, where each line in the input is expected to be a complete and valid JSON object. This is useful for processing logs or data streams.

infile
    An optional positional argument specifying a JSON file to parse. If not provided, the tool reads from standard input (stdin).

DESCRIPTION


python -m json.tool
is a built-in utility within Python's standard library that allows users to easily pretty-print, validate, and optionally minify JSON (JavaScript Object Notation) data directly from the command line. It leverages the json module, which provides robust JSON encoding and decoding capabilities.

When invoked, it reads JSON data from standard input (stdin) by default, or from a specified file. It then parses the data and outputs a beautifully formatted, human-readable version to standard output (stdout). This tool is invaluable for developers, system administrators, and anyone working with JSON, as it helps in quickly inspecting malformed JSON, making large JSON files human-readable, or reformatting them for consistency. It's a convenient way to interact with JSON without writing custom Python scripts, making it a go-to for quick JSON manipulation tasks.

CAVEATS


python -m json.tool
is a basic formatting and validation tool, not a full-fledged JSON processing language like jq. While it performs validation by raising an error on invalid input, its error messages might be generic for complex JSON errors. For very large JSON files, its performance might be slower compared to optimized native utilities. It primarily focuses on pretty-printing and assumes a valid JSON structure for successful formatting.

INPUT METHODS

The tool supports various input methods:
1. From a file: python -m json.tool mydata.json
2. From piped input: cat mydata.json | python -m json.tool or curl https://api.example.com/data | python -m json.tool
3. From interactive standard input: Simply run python -m json.tool, paste or type your JSON, then press Ctrl+D (Unix/Linux) or Ctrl+Z then Enter (Windows) to signal end of input.

OUTPUT AND REDIRECTION

The formatted JSON output is always sent to standard output (stdout). This allows for easy redirection to another file (e.g., python -m json.tool data.json > formatted_data.json) or piping to other command-line utilities for further processing (e.g., python -m json.tool data.json | less).

IMPLICIT VALIDATION

While there isn't an explicit 'validate only' option, the tool performs implicit validation. If the input data is not valid JSON, it will raise a json.JSONDecodeError and print an error message to standard error (stderr), indicating that the input could not be parsed as JSON. This makes it a quick check for JSON syntax correctness.

HISTORY

The json module, which powers json.tool, was introduced into Python's standard library with Python 2.6 (as per PEP 3108) and has been an integral part of it ever since. Its inclusion reflected the rapidly growing adoption of JSON as a primary data interchange format across web services and applications. The json.tool command-line utility was made available as a convenient wrapper around the module's core functionalities, providing an immediate, no-script solution for common JSON manipulation tasks like formatting and validation. This development underscored Python's commitment to providing robust, out-of-the-box tools for modern data handling.

SEE ALSO

jq(1), curl(1), cat(1), grep(1)

Copied to clipboard