Validate JSON5: What a Validator Checks, What Conversion Changes, and When Plain JSON Is Better
How JSON5 validation works, what changes when JSON5 is converted to strict JSON, copy-and-paste pitfalls, and when plain JSON is the safer choice.
What a JSON5 validator does
JSON5 is an extension of JSON that borrows syntax from ECMAScript 5.1 to make hand-edited files, such as configuration, easier to write and read. A JSON5 validator parses text against the JSON5 grammar and reports whether it is well-formed, usually with the location of the first error. Tools sold as a validator can do one of three different jobs: a syntax check that leaves your text untouched, a conversion that re-serializes the parsed data as strict JSON, or a schema check that tests whether the parsed values have the structure an application expects. Knowing which job a tool performs matters more than the label.
- Syntax JSON5 accepts that strict JSON rejects:
- Single-line and multi-line comments
- Object keys without quotes, when the key is a valid identifier name
- Strings in single quotes
- Trailing commas after the last item in objects and arrays
- Strings continued across lines with a backslash before the line break
- Hexadecimal numbers, a leading or trailing decimal point, and an explicit plus sign
- Infinity, -Infinity and NaN as number values
- A wider set of whitespace characters
Is JSON5 validation suitable for your use?
Validating JSON5 is a good fit when people edit a file by hand and the program that reads it uses a JSON5-aware parser. The main benefit is that comments and relaxed syntax keep configuration readable while the validator still catches broken structure before deployment. The decisive tradeoff is that passing a JSON5 check says nothing about whether the destination accepts JSON5. Strict JSON parsers, including the built-in JSON parsing in most languages, reject comments, unquoted keys, single quotes and trailing commas. A file that validates as JSON5 can therefore still fail where it is used.
- Plain JSON is the better choice when:
- Data is exchanged between systems, such as API requests and responses
- You do not know or control the parser on the receiving end
- The file is read by tooling that only documents JSON support
- Values include Infinity or NaN that the receiver must handle, since JSON has no representation for them
- The data is generated by a program rather than written by people, so comments and relaxed syntax add little
What validation changes, and what conversion changes
A pure syntax check reads the text and changes nothing. Formatting and conversion are different: they parse the text into values and then write those values out again, so the stored characters change even when the data does not. The example below shows a JSON5 input and the strict JSON a parse-and-serialize step would produce.
// service settings
{
name: 'api',
port: 8080,
maxSize: 0x1F,
ratio: .5,
hosts: [
'primary',
'replica',
],
}
{
"name": "api",
"port": 8080,
"maxSize": 31,
"ratio": 0.5,
"hosts": [
"primary",
"replica"
]
}
In the converted output the comment is gone, every key is quoted, single quotes become double quotes, the hexadecimal 0x1F becomes 31, .5 becomes 0.5, and the trailing commas are dropped. The values are equivalent; the text is not. Comments cannot be recovered from the JSON output, so keep the JSON5 file as the source and treat the JSON as a generated artifact.
| JSON5 feature | Strict JSON accepts it | Effect of converting to JSON |
| Comments | No | Removed |
| Unquoted keys | No | Keys are quoted |
| Single-quoted strings | No | Rewritten with double quotes |
| Trailing commas | No | Removed |
| Hex numbers, leading or trailing decimal point, plus sign | No | Rewritten as plain decimal numbers |
| Infinity, -Infinity, NaN | No | No JSON equivalent; JavaScript's JSON.stringify writes null, and other tools may raise an error |
| Backslash line continuation in strings | No | The line break is removed and the string is joined |
Reformatting JSON5 back into JSON5 carries a similar risk. The reference JavaScript library parses text into plain values, so comments do not survive a parse-then-stringify cycle. A formatter that keeps comments has to work on the syntax itself rather than on parsed values, and whether a given tool does this has to be checked for that tool.
Will the result stay usable after copying or sharing?
Most copy-and-paste failures come from the text changing on the way. Word processors, chat apps and some web editors replace straight quotes with typographic quotes. JSON5 accepts only straight single or double quotes as string delimiters, so a snippet that validated in your editor can fail after passing through a rich-text tool. Copy from a plain-text source or a code block when possible, and validate again after pasting.
Invisible characters are a subtler problem. Because unquoted keys follow ECMAScript identifier rules, which permit the zero-width joiner and zero-width non-joiner inside names, a key copied from rich text can parse successfully yet fail to match the key your program looks up, even though both look identical on screen. JSON5 also treats more Unicode spaces as whitespace than JSON does, including the non-breaking space, so the same raw text can pass a JSON5 check and fail a strict JSON check.
When sharing, match the format to the destination. If the recipient or system expects JSON, send the converted output rather than the JSON5 source. If you share the source, label it clearly, for example with a .json5 file extension, so editors and colleagues do not open it as JSON. Some editors flag JSON5 syntax as errors when a file is treated as JSON. Some tools also use a commented-JSON dialect, often called JSONC, that allows comments and sometimes trailing commas but not the rest of JSON5. A file valid in one dialect is not necessarily valid in the other.
For readability and accessibility, JSON5's comments and quote-free keys generally make files easier for people to scan, and a screen reader has less punctuation to announce. The parts of a validator that matter most for accessibility are error messages that give a line and column number in text, rather than relying only on color highlighting. Whether a particular validator's error output is announced by a particular screen reader can only be established by testing that combination. No such test was performed for this article.
Privacy when using an online validator
Configuration files often contain credentials, internal hostnames or tokens. A clean interface or reassuring wording does not tell you whether pasted text is logged, stored or transmitted. Rely only on a documented policy, or validate locally with a JSON5 parsing library or an editor extension so the text never leaves your machine. Remove secrets before pasting into any tool whose handling you have not confirmed.
Gaps and how to check a tool yourself
No specific JSON5 validator was tested for this review. Error-message quality, performance on large files, comment preservation and data handling were not measured for any product. The statements above describe the JSON5 format and general parsing behavior. Before relying on a tool, run a short test of your own.
- A quick test for any JSON5 validator:
- Paste a sample that uses every feature in the table above and confirm it passes
- Add one deliberate error, such as a missing colon, and check that the reported line and column are correct
- If the tool converts to JSON, confirm how it handles Infinity and NaN
- If the tool formats JSON5, check whether comments survive
- Paste text containing typographic quotes and confirm the tool rejects it
- Read the tool's documentation on whether input is sent to a server