What this JSON5 formatter does
JSON5 extends JSON with the conveniences of JavaScript object literals, which makes it pleasant for hand-written configuration. This JSON5 formatter parses such a file and prints it again in a consistent style, or converts it into standard JSON when another program needs to read it.
JSON5 features at a glance
- Comments:
// lineand/* block */. - Trailing commas in objects and arrays.
- Unquoted keys when the key is a valid identifier, and single-quoted strings.
- Numbers written as
0x1F90,.5,5.,+3,InfinityandNaN. - Strings split over several lines with a backslash at the end of each line.
Example
// server settings
{
host: 'localhost',
port: 0x1F90, // 8080
ratio: .5,
retries: +3,
timeout: Infinity,
"log-level": "debug",
paths: ['/api', '/admin',],
}
Format JSON5 with the defaults (2 spaces, single quotes) gives:
{
host: 'localhost',
port: 8080,
ratio: 0.5,
retries: 3,
timeout: Infinity,
'log-level': 'debug',
paths: [
'/api',
'/admin',
],
}
Look closely at what changed. Keys that are valid identifiers are unquoted and log-level, which contains a hyphen, keeps its quotes. Numbers are normalized: the hex value is written in decimal, .5 becomes 0.5 and the plus sign is dropped. Every multi-line object and array ends with a trailing comma, which keeps future diffs to one line.
Comments are not preserved
The formatter parses the file into data and prints the data again, so comments are dropped, as you can see above. If your comments matter, format a copy, or use the output only to check structure and indentation. The same applies to the original number spelling: 0x1F90 is stored as the number 8080.
Options
- Indent – 2, 3 or 4 spaces or a tab.
- String quotes – single quotes (the default) or double quotes for all strings and for keys that need quoting.
- Sort keys – orders keys alphabetically at every level, ignoring case, while arrays keep their order.
The quote setting is applied to every string, and the chosen quote character is escaped where it appears inside a value. With single quotes, "it's" is written as 'it\'s'; with double quotes it stays "it's". If your text has many apostrophes, double quotes keep it cleaner.
Duplicate keys are allowed by the parser, but only the last value survives: {a: 1, a: 2} is formatted as {a: 2}. Formatting is therefore a quick way to spot a key that was accidentally defined twice in a long config.
Convert JSON5 to JSON
Convert to JSON outputs strict JSON: all keys double-quoted, no comments and no trailing commas. JSON has no representation for Infinity or NaN, so those values become null; in the example, "timeout": null. Check such fields before relying on the converted file.
Validate checks the syntax and says whether the document is also valid strict JSON. For a page dedicated to checking files, with a list of typical errors, see the JSON5 validator.
JSON5, JSON or YAML?
JSON5 suits configuration that people edit by hand and a JavaScript toolchain reads. For data exchanged between services, stick to plain JSON and format it with the JSON formatter. If you want comments plus a more compact syntax, compare with YAML in the YAML formatter.
Formatting and conversion run in your browser; the file is not uploaded unless you use Load URL or Save & Share.