LinuxCommandLibrary

json5

Convert JSON5 data to JSON

TLDR

Convert JSON5 stdin to JSON stdout

$ echo [input] | json5
copy

Convert a JSON5 file to JSON and output to stdout
$ json5 [path/to/input_file.json5]
copy

Convert a JSON5 file to the specified JSON file
$ json5 [path/to/input_file.json5] --out-file [path/to/output_file.json]
copy

Validate a JSON5 file
$ json5 [path/to/input_file.json5] --validate
copy

Specify the number of spaces to indent by (or "t" for tabs)
$ json5 --space [indent_amount]
copy

Display help
$ json5 --help
copy

SYNOPSIS

json5 [options] [file...]
json5 [options] < input.json5

PARAMETERS

-v, --version
    Displays the version number of the json5 utility.

-s, --stringify
    Parses the input JSON5 and outputs it as standard (minified) JSON. This is often the default behavior if no other output-related options are specified.

-p, --pretty [indent]
    Pretty-prints the JSON5 or JSON output. The optional indent argument specifies the number of spaces to use for indentation (default is 2). Use 0 for no indentation (similar to --stringify but might retain some structure).

-V, --validate
    Validates the input JSON5 syntax without producing any output. The command exits with a status code of 0 for valid JSON5 and 1 for invalid JSON5.

-h, --help
    Displays the usage information and a list of available options for the json5 command.

DESCRIPTION

The json5 command-line utility provides a convenient way to work with JSON5 (JavaScript Object Notation, Fifth Edition) files. JSON5 is a superset of JSON, offering features like comments, trailing commas, unquoted keys, single-quoted strings, and hexadecimal numbers, making configuration files and data structures more human-readable and writable.

This command can read JSON5 input from standard input or specified files, then perform various operations: it can convert JSON5 into standard JSON, pretty-print JSON5 content, or simply validate its syntax without producing any output. It is particularly useful for environments where configuration files benefit from the relaxed syntax of JSON5, allowing for clearer and more maintainable data.

CAVEATS

The json5 command-line utility is typically implemented as a Node.js package and thus requires a Node.js runtime environment to be installed on the system. It is not a standard utility found in most Linux distributions by default and usually needs to be installed via a package manager like `npm` (e.g., `npm install -g json5`). It is specifically designed for JSON5 and does not offer the extensive querying and manipulation capabilities of tools like jq for generic JSON.

INPUT/OUTPUT

By default, json5 reads from standard input if no files are specified. If one or more file arguments are provided, it reads content from those files. Output is directed to standard output. When --validate is used, there is no output to standard output, only an exit code indicating success or failure.

EXIT STATUS

The json5 command typically exits with a status of 0 on success (e.g., successful parsing, stringification, or validation). A non-zero exit status (e.g., 1) indicates an error, such as invalid JSON5 input, file access issues, or other runtime errors, especially when using the --validate option.

HISTORY

JSON5 was conceived as a human-friendly alternative to JSON, addressing its strictness for use cases like configuration files. The project aims to make data interchange formats easier for humans to write and maintain, while still being compatible with the JavaScript ecosystem. The json5 command-line tool emerged as a practical utility to bridge the gap between JSON5 and standard JSON, allowing developers to leverage the enhanced readability of JSON5 in their workflows, often by converting it to standard JSON for consumption by stricter parsers or APIs. Its development closely tracks the evolution of the JSON5 specification, providing a reliable parser and serializer.

SEE ALSO

jq(1), cat(1), grep(1), sed(1)

Copied to clipboard