What the JSON5 Formatter does
The JSON5 Formatter reads JSON5, the relaxed superset of JSON that allows comments, trailing commas, single-quoted strings and unquoted keys, and writes it out with consistent indentation using the JSON5 library.
JSON5 Formatter examples
The built-in sample
Input
{"name":"Ada Lovelace","active":true,"tags":["math","code"],"address":{"city":"London"}}Output
{
name: 'Ada Lovelace',
active: true,
tags: [
'math',
'code',
],
address: {
city: 'London',
},
}This is the input that the Load sample button fills in.
Comments, hexadecimal numbers and trailing commas
Input
{
// retry settings
retries: 3,
'base-path': '/api/v2',
mask: 0xFF,
ratio: .5,
hosts: ['a', "b",],
}Output
{
retries: 3,
'base-path': '/api/v2',
mask: 255,
ratio: 0.5,
hosts: [
'a',
'b',
],
}The comment is dropped, 0xFF is written as 255 and .5 as 0.5. Strings come out single-quoted, a key is quoted only when it is not a valid identifier, and a trailing comma is added after the last item of each object and array.
Each output was produced by running this tool on the input above it.
How it works
The JSON5 library parses the input, accepting comments, trailing commas, single-quoted strings, unquoted keys, hexadecimal numbers and the other JSON5 extensions. The parsed data is then written out again with two-space indentation.
Details and limits
- The output is JSON5, not JSON: single-quoted strings, unquoted identifier keys and trailing commas. A strict JSON parser will reject it.
- Comments are lost, because the text is rebuilt from the parsed data.
- Number spellings are normalised: hexadecimal becomes decimal and a leading or trailing decimal point gets its zero.
InfinityandNaNare valid JSON5 numbers and are written back unchanged.- Input that is not valid JSON stops the tool, and the status line shows where the problem is. The JSON Validator page lists the usual causes.
Questions
How do I get strict JSON from a JSON5 file?
This tool writes JSON5. To get strict JSON, load the file with a JSON5 library in your own code and write it with JSON.stringify.