JSON vs JSON5: What JSON5 Adds and When to Use It
JSON5 is an extension of JSON designed to be easier for people to write by hand. Every valid JSON document is also valid JSON5, but not the other way round. This guide lists exactly what JSON5 adds and where each format belongs.
What JSON5 adds
{
// comments, single-line and /* block */
unquoted: 'and single-quoted strings',
trailing: [1, 2, 3,],
hex: 0xFF,
leadingDot: .5,
positive: +1,
special: [Infinity, -Infinity, NaN],
multiline: 'line one \
line two',
}- Comments (
//and/* */). - Unquoted object keys when they are valid identifiers.
- Single-quoted strings.
- Trailing commas in objects and arrays.
- Hexadecimal numbers, leading or trailing decimal points, a leading plus sign, and
InfinityandNaN. - Strings that continue over several lines with a backslash.
What stays the same
The data model is unchanged: objects, arrays, strings, numbers, booleans and null. JSON5 changes how you can write data, not what data you can represent.
When to use JSON5
JSON5 is useful for configuration files that people edit and want to annotate with comments. Some tools accept it, and others accept a similar "JSON with comments" variant (such as editor settings files).
When to stick to JSON
Use standard JSON for anything exchanged between programs, especially APIs. Standard JSON parsers, including JSON.parse, reject JSON5 features, and a document that works in one tool may fail in another. If you have JSON5, convert it to JSON before sending it: format it with the JSON5 Formatter, check it with the JSON5 Validator, then remove comments and trailing commas.
Telling the two apart from an error
If the JSON Validator reports an unexpected token at a /, a single quote or a closing bracket after a comma, the file is probably JSON5 or "JSON with comments". Validate it with the JSON5 Validator instead.
Worked example: Comparing a config file written in JSON5 vs strict RFC 8259 JSON
Here is the exact same application configuration written first in human-edited JSON5 and then compiled down to strict JSON:
// JSON5 configuration (comments, unquoted keys, trailing commas, hex)
{
appName: 'ConvertFerry Worker',
maxUploadBytes: 0x100000, // 1 MiB in hex
allowedOrigins: [
'https://www.convertferry.com',
],
}
When converted to strict RFC 8259 JSON for JSON.parse() or an HTTP API body, all // comments are stripped, keys and strings are double-quoted, hexadecimal 0x100000 is converted to base-10 1048576, and trailing commas before ] and } are removed:
{
"appName": "ConvertFerry Worker",
"maxUploadBytes": 1048576,
"allowedOrigins": [
"https://www.convertferry.com"
]
}Limits and where JSON5 cannot be used
- Native browser
JSON.parse()andfetch().json()reject JSON5: Every web browser's built-inJSON.parseimplements strict RFC 8259 JSON. Sending comments, single quotes, or trailing commas over a REST API will throw aSyntaxErrorunless the client explicitly imports thejson5npm library. - PostgreSQL
jsonband MySQLJSONcolumns require strict JSON: Database JSON columns andjqCLI pipelines only accept standard JSON. - JSONC (VS Code JSON with Comments) is not full JSON5: VS Code
tsconfig.jsonandsettings.jsonuse *JSONC* (which allows//comments and trailing commas, but still requires double quotes around keys and strings, unlike JSON5).
Frequently asked questions
Is JSON5 an official standard?
It has a published specification maintained by the JSON5 project, but it is not an IETF or ECMA standard like JSON.
Can JSON.parse read JSON5?
No. Use a JSON5 library such as the one these tools use.
Is "JSON with comments" the same as JSON5?
No. It usually means JSON plus comments (and sometimes trailing commas), without the other JSON5 extensions.
Is every valid JSON file also a valid JSON5 file?
Yes. JSON5 is a strict superset of RFC 8259 JSON (and a subset of ECMAScript 5.1 object literal syntax), so any valid .json file parses without changes in a JSON5 parser.
How do multi-line strings work in JSON5?
In JSON5, you can split a long string across multiple physical lines by placing a backslash (\) right before the newline character at the end of each line.
How can I convert a JSON5 file into standard JSON?
Paste or validate your file using our JSON5 Formatter and JSON5 Validator tools, or compile it to standard JSON before sending it to an API.