JSON 13 📄 JSON5 Features — Comments, Trailing Commas, Unquoted Keys
JSON5 is not a replacement for JSON. It is a proposed extension that keeps the JSON data model intact—no new types, no new semantics—and relaxes the syntax so that a human can write the configuration files without fighting the parser. The official README states the motivation plainly: JSON “isn’t the friendliest to write. Keys need to be quoted, objects and arrays can’t have trailing commas, and comments aren’t allowed—even though none of these are the case with regular JavaScript today” . JSON5 adds exactly those missing conveniences, and it adds them from ECMAScript 5.1, which means every valid JSON5 file is also a valid ES5 expression.
The three features that define JSON5 for most projects are the comments, the trailing commas, and the unquoted keys. The comments make the configuration files self-documenting. The trailing commas make the diffs clean when a list is reordered or extended. The unquoted keys remove the visual noise that the quotes create around the identifiers that are already valid JavaScript names. The specification lists more features—the single-quoted strings, the hexadecimal numbers, the Infinity and the NaN, the leading and trailing decimal points—but the three are the ones that change the way the files look and feel.
Key point: JSON5 is a superset of JSON, so every valid JSON file is a valid JSON5 file. It adds the comments, the trailing commas, the unquoted keys, the single-quoted strings, the hexadecimal numbers, the leading and trailing decimal points, the Infinity/NaN, the explicit plus sign, and the multi-line strings. It is not an official successor to JSON, and the JSON5 content may not work with the existing JSON parsers, which is why the JSON5 files use the .json5 extension .
Why JSON5 exists
The configuration problem. JSON was designed as a data interchange format, and the data interchange is the machine-to-machine concern. The format’s strictness is a feature for the machines: the quotes around the keys, the absence of the comments, and the rejection of the trailing commas make the parsing unambiguous and the security simple. But JSON’s usage has expanded beyond the machines. It is now used for the configuration files, the manifests, and the tests, and those are written by the humans . The humans want the comments to explain the non-obvious settings, the trailing commas to keep the diffs clean, and the unquoted keys to reduce the noise. JSON5 is the format that gives them those, and it does it without leaving the JSON family.
The YAML comparison problem. A human-friendly format already exists: YAML. But the YAML has its own problems—the indentation sensitivity, the implicit typing, the complex specification—and the migration from JSON to YAML is a disruptive change for a project that already has the JSON tooling. JSON5’s aim is to “remain close to JSON and JavaScript” . The format is the minimal delta, and the minimal delta is the reason it is adopted by the projects that want the comments and the trailing commas without the full YAML.
The ES5 subset problem. JSON5 is a strict subset of ES5, which means a JSON5 file is also a valid JavaScript expression. The eval of a JSON5 file is a valid JavaScript evaluation, and the JSON5 parser is a JavaScript parser with the JSON5 grammar. The subset property is the mechanism that makes the format familiar to the JavaScript developers, and it is the reason the JSON5 files can be required directly in Node.js with the require hook .
The compatibility problem. JSON5 is a superset of JSON, so the existing JSON files are valid JSON5 files. The transition is the direction that works: a project can rename the .json files to .json5, add the comments, and the parser still reads them. The reverse is not true: a JSON5 file with the comments is not a valid JSON file, and the JSON parser rejects it. The .json5 extension is the mechanism that signals the parser choice, and the CLI is the mechanism that converts the JSON5 to the JSON for the tools that require the strict JSON .
The performance trade-off. The JSON5 parser is not the fastest parser, and the README states that the library is used “where e.g. extreme performance isn’t necessary” . The format is for the configuration files, not for the high-throughput APIs. The trade-off is the readability against the parsing speed, and the JSON5 is the choice for the files that the humans read and write, not for the files that the machines exchange at the scale.
a. The comments and the trailing commas
The comments are the two forms: the single-line comment and the block comment. The single-line comment starts with the // and continues to the end of the line. The block comment starts with the /* and ends with the */, and it can span the multiple lines. The comments are the mechanism that documents the configuration file, and they are the feature that the JSON lacks entirely .
// The single-line comment
{
// The database configuration
database: {
host: 'localhost', // The local development host
port: 5432,
},
/* The block comment
that spans the multiple lines */
cache: {
enabled: true,
},
}
The trailing commas are allowed in the objects and the arrays. The comma after the last member is the valid JSON5, and the parser ignores it. The trailing comma is the mechanism that keeps the diffs clean when the last member is added or removed, and it is the feature that the JSON rejects .
// The trailing comma in the object
{
name: 'Ada',
age: 36, // The trailing comma is allowed
}
// The trailing comma in the array
[
'first',
'second', // The trailing comma is allowed
]
The trailing comma is the single comma, not the multiple. The specification says “objects may have a single trailing comma” and “arrays may have a single trailing comma” . The [[1, 2, 3,,]] is the invalid JSON5, because the double comma is the empty element, not the trailing comma. The parser rejects it.
The comments and the trailing commas are the two features that the JSON5 inherits from the JavaScript. The JavaScript has always allowed them, and the JSON5 is the JSON with the JavaScript’s conveniences. The inheritance is the mechanism that makes the format familiar, and the familiarity is the reason the format is adopted.
b. The unquoted keys and the strings
The unquoted keys are the identifiers without the quotes. The key can be unquoted if it is a valid ECMAScript 5.1 IdentifierName, which includes the reserved keywords like default, while, and class . The unquoted key is the mechanism that removes the visual noise, and it is the feature that makes the configuration files look like the JavaScript objects.
// The unquoted keys
{
name: 'Ada', // The name is a valid identifier
default: true, // The reserved keyword is a valid unquoted key
while: false, // The while is a valid unquoted key
'quoted-key': 1, // The hyphen is not a valid identifier, so the quotes are required
}
The hyphenated key, the key with the space, and the key with the special character are the cases where the quotes are required. The 'quoted-key' and the "quoted key" are the valid JSON5, and the quoted-key without the quotes is the invalid JSON5. The rule is the same as the JavaScript object literal: the identifier is unquoted, the non-identifier is quoted.
The single-quoted strings are the second string form. The JSON5 allows the single quotes as well as the double quotes, and the parser accepts both . The single quote is the mechanism that lets the string contain the double quotes without the escaping, and it is the convenience that the JSON lacks.
// The single-quoted string
{
message: 'He said "hello"', // The double quotes inside the single quotes
quoted: "It's a test", // The single quote inside the double quotes
}
The multi-line strings are the third string feature. The string can span the multiple lines if each newline is escaped with the backslash. The escaped newline is removed, and the string continues on the next line .
// The multi-line string
{
message: 'This is a \
multi-line string', // The backslash escapes the newline
}
The string features are the convenience, and the convenience is the reason the JSON5 is the configuration format. The strings are the data, and the data is the human-readable text. The single quotes and the multi-line strings are the mechanism that makes the text readable, and the readability is the point.
Complete Example Session
// ============================================
// PART 1: THE BASIC CONFIGURATION
// ============================================
// The config.json5 file
{
// The application name
name: 'my-app',
// The server configuration
server: {
host: 'localhost',
port: 3000,
},
}
// The unquoted keys, the single quotes, the comments.
// ============================================
// PART 2: THE TRAILING COMMAS
// ============================================
{
name: 'Ada',
age: 36, // The trailing comma is allowed
tags: [
'math',
'code', // The trailing comma is allowed
],
}
// The trailing comma is the single comma.
// The double comma is the empty element and the invalid.
// ============================================
// PART 3: THE UNQUOTED KEYS
// ============================================
{
name: 'Ada', // The valid identifier
default: true, // The reserved keyword is valid
while: false, // The reserved keyword is valid
'quoted-key': 1, // The hyphen requires the quotes
'quoted key': 2, // The space requires the quotes
}
// The identifier is unquoted.
// The non-identifier is quoted.
// ============================================
// PART 4: THE SINGLE-QUOTED STRINGS
// ============================================
{
message: 'He said "hello"', // The double quotes inside
quoted: "It's a test", // The single quote inside
}
// The single quote lets the string contain the double quotes.
// ============================================
// PART 5: THE MULTI-LINE STRING
// ============================================
{
message: 'This is a \
multi-line string',
}
// The backslash escapes the newline.
// ============================================
// PART 6: THE HEXADECIMAL NUMBER
// ============================================
{
color: 0xFF0000, // The hexadecimal
mask: 0b1010, // The binary is also allowed
}
// The hexadecimal is the base-16 number.
// ============================================
// PART 7: THE INFINITY AND THE NAN
// ============================================
{
max: Infinity, // The infinity
min: -Infinity, // The negative infinity
invalid: NaN, // The NaN
}
// The Infinity and the NaN are the valid JSON5 numbers.
// ============================================
// PART 8: THE LEADING DECIMAL POINT
// ============================================
{
half: .5, // The leading decimal point
whole: 5., // The trailing decimal point
positive: +1, // The explicit plus sign
}
// The .5 and the 5. are the valid numbers.
// ============================================
// PART 9: THE PARSER
// ============================================
import JSON5 from 'json5';
const config = JSON5.parse(`
{
// The database
database: {
host: 'localhost',
port: 5432,
},
}
`);
// The JSON5.parse reads the comments and the unquoted keys.
// The result is the JavaScript object.
// ============================================
// PART 10: THE CHECKLIST
// ============================================
// [ ] The file extension is .json5.
// [ ] The comments are the // and the /* */.
// [ ] The trailing commas are the single comma.
// [ ] The unquoted keys are the valid identifiers.
// [ ] The single-quoted strings are allowed.
// [ ] The hexadecimal numbers are allowed.
// [ ] The Infinity and the NaN are allowed.
The ten parts covered the basic configuration, the trailing commas, the unquoted keys, the single-quoted strings, the multi-line string, the hexadecimal number, the Infinity and the NaN, the leading decimal point, the parser, and the checklist. The through-line is that the features are the JavaScript conveniences, and the format is the JSON with those conveniences.
Quick Reference
The JSON5 features
| Feature | JSON | JSON5 |
|---|---|---|
| The comments | No | The // and the /* */ |
| The trailing commas | No | The objects and the arrays |
| The unquoted keys | No | The valid identifiers |
| The single-quoted strings | No | Yes |
| The hexadecimal numbers | No | Yes |
The Infinity / NaN | No | Yes |
| The leading decimal point | No | Yes |
| The multi-line strings | No | Yes |
The compatibility
| Direction | Valid |
|---|---|
| JSON → JSON5 | Yes, the JSON is the subset |
| JSON5 → JSON | No, the comments and the unquoted keys are invalid |
| JSON5 → ES5 | Yes, the strict subset |
The parser
| API | Purpose |
|---|---|
JSON5.parse | The parse with the reviver |
JSON5.stringify | The stringify with the replacer |
The require hook | The .json5 file require |
Best Practices
✅ Do This:
// Use the comments to document the config
{
// The database host
host: 'localhost',
}
// Use the trailing comma for the clean diffs
{
name: 'Ada',
age: 36,
}
❌ Don’t Do This:
// Use the double trailing comma
{
name: 'Ada',, // The invalid JSON5
}
// Use the hyphen in the unquoted key
{
quoted-key: 1, // The invalid JSON5, the quotes are required
}
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| The JSON parser rejects the file | The file has the comments or the unquoted keys | Use the JSON5 parser or the .json5 extension |
| The unquoted key is rejected | The key is not a valid identifier | Quote the key |
| The trailing comma is rejected | The parser is the strict JSON | Use the JSON5 parser |
| The double comma is rejected | The empty element is not the trailing comma | Remove the extra comma |
Real-World Examples
1. The comment
// The single-line comment
2. The block comment
/* The block comment */
3. The trailing comma
{ name: 'Ada', }
4. The unquoted key
{ name: 'Ada' }
5. The quoted key
{ 'quoted-key': 1 }
6. The single quote
{ message: 'He said "hello"' }
7. The multi-line string
{ message: 'Line 1 \
Line 2' }
8. The hexadecimal
{ color: 0xFF0000 }
9. The Infinity
{ max: Infinity }
10. The parse
JSON5.parse(configString)
Visual
The JSON vs JSON5
┌──────────────────────────────────────────────────────────────┐
│ JSON VS JSON5 │
│ │
│ JSON JSON5 │
│ ┌─────────────────────┐ ┌─────────────────────┐ │
│ │ "name": "Ada" │ │ name: 'Ada' │ │
│ │ No comments │ │ // The comment │ │
│ │ No trailing comma │ │ name: 'Ada', │ │
│ └─────────────────────┘ └─────────────────────┘ │
│ │
│ The JSON is the strict subset. │
│ The JSON5 is the superset with the JavaScript conveniences. │
└──────────────────────────────────────────────────────────────┘
The Unquoted Keys
┌──────────────────────────────────────────────────────────────┐
│ THE UNQUOTED KEYS │
│ │
│ ✅ The valid identifier: │
│ ┌─────────────────────┐ │
│ │ name: 'Ada' │ The identifier │
│ │ default: true │ The reserved keyword is valid │
│ └─────────────────────┘ │
│ │
│ ❌ The non-identifier: │
│ ┌─────────────────────┐ │
│ │ 'quoted-key': 1 │ The hyphen requires the quotes │
│ │ 'quoted key': 2 │ The space requires the quotes │
│ └─────────────────────┘ │
└──────────────────────────────────────────────────────────────┘
The Compatibility
┌──────────────────────────────────────────────────────────────┐
│ THE COMPATIBILITY │
│ │
│ JSON → JSON5 │
│ ┌─────────────────────┐ │
│ │ The valid JSON is │ The JSON5 is the superset │
│ │ the valid JSON5 │ │
│ └─────────────────────┘ │
│ │
│ JSON5 → JSON │
│ ┌─────────────────────┐ │
│ │ The comments and │ The JSON parser rejects them │
│ │ the unquoted keys │ The .json5 extension is the │
│ │ are invalid JSON │ signal │
│ └─────────────────────┘ │
└──────────────────────────────────────────────────────────────┘
Summary
| Item | Value |
|---|---|
| The format | The JSON5 Data Interchange Format |
| The relationship | The superset of JSON, the strict subset of ES5 |
| The comments | The // and the /* */ |
| The trailing commas | The objects and the arrays |
| The unquoted keys | The ECMAScript 5.1 IdentifierName |
| The single quotes | The alternative to the double quotes |
| The hexadecimal | The base-16 numbers |
| The extension | The .json5 |
Key takeaways:
- JSON5 is a superset of JSON with the ECMAScript 5.1 syntax. The valid JSON files are the valid JSON5 files, and the JSON5 adds the comments, the trailing commas, the unquoted keys, the single quotes, the hexadecimal numbers, the
Infinity/NaN, and the multi-line strings . - The unquoted keys must be the valid ECMAScript 5.1
IdentifierName. Thename, thedefault, and thewhileare the valid unquoted keys, and the hyphenated or the spaced keys require the quotes . - The trailing comma is the single comma. The
[1, 2, 3,]is the valid JSON5, and the[1, 2, 3,,]is the invalid because the double comma is the empty element . - JSON5 is not the official successor to JSON. The JSON5 content may not work with the existing JSON parsers, and the
.json5extension is the signal for the parser choice. The CLI is the mechanism that converts the JSON5 to the JSON for the tools that require the strict JSON . - The format is for the human-written files, not the machine-to-machine exchange. The README states that the format is “not intended to be used for machine-to-machine communication” and that the performance is not the priority . The JSON5 is the configuration format, and the JSON is the data interchange format.
Remember: The JSON5 is the JSON with the JavaScript conveniences. The comments, the trailing commas, and the unquoted keys are the features that make the configuration files readable and maintainable. The format is the superset of JSON and the strict subset of ES5, and the .json5 extension is the signal for the parser. The JSON5 is for the humans, and the JSON is for the machines.
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!