LinuxCommandLibrary

jsonlint

Validate JSON data syntax and format

SYNOPSIS

jsonlint [options] [file...]

file...: One or more JSON files to process. If no files are specified, jsonlint reads from standard input (stdin).

PARAMETERS

--help, -H
    Displays usage information and exits.

--version, -v
    Outputs the current version number of jsonlint.

--compact, -c
    Outputs the JSON in a compact, minified format (no whitespace or line breaks).

--pretty, -p
    Outputs the JSON in a human-readable, pretty-printed format (often the default behavior).

--indent N, -i N
    Specifies the number of spaces (N) to use for indentation when pretty-printing. The default is typically 2 spaces.

--indent-with-tabs, -t
    Uses tab characters instead of spaces for indentation when pretty-printing.

--sort-keys, -o
    Sorts the keys within JSON objects alphabetically, ensuring consistent output.

--silent, -s
    Suppresses output on successful validation or formatting. Only error messages are printed.

--quiet, -q
    Suppresses all output, including errors. The success or failure can only be determined by the exit status.

--validate, -V
    Strictly validates the input as JSON without performing any reformatting, primarily checking for syntax correctness.

DESCRIPTION

jsonlint is a command-line utility designed for validating and formatting JSON (JavaScript Object Notation) data.

It meticulously checks if a given JSON input adheres to the strict JSON specification, reporting any syntax errors or structural issues, thereby ensuring data integrity. Beyond mere validation, jsonlint also offers robust reformatting capabilities, allowing users to pretty-print JSON with consistent indentation for enhanced readability, or conversely, to compact it into a single line for storage and transmission efficiency.

This indispensable tool is widely used by developers and system administrators working with JSON-based APIs, configuration files, and data serialization. It can process input from standard input (stdin) or directly from specified files, making it highly flexible for integration into shell scripts, automated workflows, and data pipelines.

CAVEATS

It's important to note that different distributions or packages might provide varying implementations of jsonlint (e.g., Node.js-based, Python-based), which may lead to slight differences in available options or specific behaviors.

Additionally, jsonlint primarily focuses on syntactic validation of JSON. It does not perform semantic validation, meaning it won't check if the JSON content conforms to a specific schema or data model, only if its structure is valid JSON.

<B>USAGE EXAMPLES</B>

Here are some common ways to use jsonlint:

Validate JSON from standard input:
echo '{"name": "Alice", "age": 30}' | jsonlint

Validate and pretty-print a JSON file:
jsonlint mydata.json

Minify a JSON file to a new file:
jsonlint --compact input.json > minified.json

Pretty-print with 4-space indentation:
jsonlint --indent 4 config.json

Validate quietly for scripting:
if jsonlint --quiet some_api_response.json; then echo "Valid JSON"; else echo "Invalid JSON"; fi

<B>EXIT STATUS</B>

jsonlint typically utilizes standard Unix exit codes to indicate the outcome of its operation:

0: Indicates that the input JSON was successfully validated and/or formatted.
1 (or any non-zero value): Indicates that an error occurred, such as invalid JSON syntax or an issue with file access. This makes jsonlint highly suitable for use in shell scripts to automate validation checks.

HISTORY

The development of jsonlint as a command-line tool closely followed the rapid adoption of JSON as a ubiquitous data interchange format.

While JSON itself originated from JavaScript, jsonlint implementations quickly emerged across various programming language ecosystems (like Node.js, Python, Ruby) to provide developers with a quick and reliable way to validate and pretty-print JSON data directly from the terminal or within build pipelines.

Its straightforward utility for ensuring data integrity and improving readability made it an essential tool for anyone regularly interacting with JSON.

SEE ALSO

jq(1), python(1), cat(1), grep(1)

Copied to clipboard