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] [[-o|--out-file]] [path/to/output_file.json]
copy

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

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

Display help
$ json5 [[-h|--help]]
copy

SYNOPSIS

json5 [-h | -V] [-c] [-i N] [--validate] [--output=json|json5] [file | -]

PARAMETERS

-h, --help
    Display help and exit

-V, --version
    Display version information

-c, --compact
    Output compact form (no whitespace or newlines)

-i N, --indent N
    Set indentation spaces (default: 2, 0 for compact)

--validate
    Validate input only; no output produced

--output FORMAT
    Output format: json (default) or json5


    Input JSON5 file(s); - for stdin (default)

DESCRIPTION

The json5 command is a lightweight command-line utility for working with JSON5, a human-readable superset of JSON. JSON5 extends standard JSON by supporting comments (single-line // and multi-line /* */), unquoted keys, trailing commas in objects and arrays, optional trailing commas after the last property, non-decimal bases (0x, 0o, 0b), and flexible date strings.

json5 reads JSON5 from files or stdin, validates syntax, and outputs either canonical JSON (for strict compatibility) or prettified JSON5. It's ideal for config files like .json5 extensions in projects, ensuring valid output for tools expecting plain JSON. Install via cargo install json5 (Rust) or npm equivalents. Processes single files or slurps multiple inputs. No built-in querying like jq, focuses on parse/format/validate.

Common use: pipe config through json5 for formatting, or convert JSON5 to JSON for legacy systems. Supports indentation customization for readability.

CAVEATS

Not installed by default on most distros; requires Rust toolchain or package manager (e.g., cargo, npm). Limited to parse/format—no filtering or querying. Large files may consume significant memory.

EXAMPLE USAGE

json5 config.json5 # format to stdout
cat data.json5 | json5 --output=json > data.json # convert to JSON
json5 --validate *.json5 # check multiple files

JSON5 VS JSON

JSON5 adds: unquoted keys (e.g., foo: 1), comments, trailing commas everywhere, hex/octal/binary nums, single-quoted strings.

HISTORY

JSON5 spec released in 2013 by Aseem Kishore, Powell and others for more flexible configs. CLI json5 tools emerged ~2015+; popular Rust impl by zqzdev (2020s) via crates.io. Evolved for modern JS/Rust ecosystems.

SEE ALSO

jq(1), json_pp(1), yq(1)

Copied to clipboard