JSON5
JSON5 is an extension of JSON that adds features from JavaScript's ES5 standard. It allows comments, trailing commas, single-quoted strings, and other conveniences that standard JSON forbids. JSON5 files are valid for humans to write but cannot be parsed by a standard JSON parser.
Why JSON5 Was Created
JSON is strict by design — machines read it perfectly. But humans writing JSON by hand face frustrating limitations. You cannot add a comment to explain a setting. You get a parse error if you leave a trailing comma after the last item. You must use double quotes everywhere.
JSON5 relaxes these rules for situations where humans write the JSON directly, like configuration files. Think of JSON as a strict grammar teacher who marks every deviation as wrong. JSON5 is a flexible editor who understands what you mean even with a comma at the end of a sentence.
JSON5 vs JSON: Side-by-Side Comparison
// This is valid JSON5 but INVALID standard JSON:
{
// Database configuration
host: 'localhost', // single-quoted string
port: 5432,
name: "shopDB", // double-quoted also fine
maxConnections: 10, // trailing comma after last field
/* Multi-line comment:
These settings apply to production */
ssl: true,
timeout: .5, // leading decimal point (0.5)
retries: +3, // explicit positive sign
description: "Line 1\
Line 2", // multi-line string using backslash
hex: 0xFF, // hexadecimal number
}
// Equivalent valid standard JSON:
{
"host": "localhost",
"port": 5432,
"name": "shopDB",
"maxConnections": 10,
"ssl": true,
"timeout": 0.5,
"retries": 3,
"description": "Line 1\nLine 2",
"hex": 255
}
JSON5 Features in Detail
1. Comments
JSON5 supports both single-line and multi-line comments.
{
// This is a single-line comment
"apiVersion": "v2",
/* This is a
multi-line comment */
"timeout": 30
}
2. Trailing Commas
Standard JSON throws an error if you put a comma after the last item in an object or array. JSON5 allows this.
{
"name": "Ritu",
"age": 28, // trailing comma - valid in JSON5
}
[
"apple",
"mango",
"banana", // trailing comma - valid in JSON5
]
3. Unquoted Keys
JSON5 allows object keys without quotes if they are valid JavaScript identifiers.
{
name: "Ritu", // no quotes around key
age: 28,
isActive: true
}
4. Single-Quoted Strings
Both single quotes and double quotes work for string values and keys.
{
'city': 'Bangalore',
"country": "India"
}
5. Multi-line Strings
JSON5 allows string values to span multiple lines using a backslash at the end of each line.
{
"address": "123, Main Street,\
Koramangala,\
Bangalore"
}
6. Special Number Values
JSON5 supports Infinity, -Infinity, and NaN as numeric values — which standard JSON does not allow.
{
"maxScore": Infinity,
"minScore": -Infinity,
"invalidValue": NaN,
"positiveNum": +42,
"hexValue": 0xFF // 255 in decimal
}
Diagram: JSON5 Feature Map
JSON5 Extras Over Standard JSON
---------------------------------
Comments → // single-line /* multi-line */
Trailing commas → Allowed in objects and arrays
Unquoted keys → name: "value" instead of "name": "value"
Single quotes → 'value' works just like "value"
Multi-line str → "line1\
line2"
Special numbers → Infinity, -Infinity, NaN, +42, 0xFF
Leading decimal → .5 instead of 0.5
Installing the JSON5 Library
Node.js
npm install json5
Usage in Node.js
const JSON5 = require('json5');
// Parsing JSON5 string
const config = JSON5.parse(`{
// Server settings
host: 'localhost',
port: 8080,
debug: true,
}`);
console.log(config.host); // localhost
console.log(config.port); // 8080
// Stringifying to JSON5
const output = JSON5.stringify(config, null, 2);
console.log(output);
Reading a .json5 File
const fs = require('fs');
const JSON5 = require('json5');
const content = fs.readFileSync('./config.json5', 'utf8');
const config = JSON5.parse(content);
console.log(config);
Python
pip install json5
import json5
# Parse JSON5 string
config = json5.loads("""
{
// App config
host: 'localhost',
port: 8080,
debug: true,
}
""")
print(config['host']) # localhost
# Dump to JSON5
output = json5.dumps(config, indent=2)
print(output)
Where JSON5 Is Used
VS Code Settings and Configuration Files
VS Code's own configuration files use JSONC (JSON with Comments), which overlaps significantly with JSON5's comment support. Many developer tools accept JSON5 or JSONC for config files because developers need to add comments.
Babel Configuration (.babelrc.js converted to JSON5)
Babel, a JavaScript compiler, originally used .babelrc files in JSON format. Developers complained about lack of comments. Some projects switched to JavaScript config files or JSON5 to add comments.
Application Configuration
// app.json5 - Human-written config file
{
// Feature flags - set to true to enable
features: {
darkMode: true,
betaEditor: false, // Not ready for production yet
analyticsV2: true,
},
// API settings
api: {
baseUrl: 'https://api.myapp.com',
timeout: 5000, // milliseconds
retries: 3,
},
}
JSON5 File Extension
JSON5 files use the .json5 extension. This signals to editors, parsers, and build tools that the file uses JSON5 syntax and not standard JSON.
Limitations of JSON5
Not Universally Supported
Standard JSON parsers — like JSON.parse() in JavaScript, json.loads() in Python, or any REST API — cannot read JSON5. If you send a JSON5 file to an API expecting standard JSON, it will fail.
Less Tooling
JSON validators, formatters, and most database import tools expect standard JSON. JSON5 requires the JSON5-specific library to parse.
Not a W3C or IETF Standard
Standard JSON has an official RFC (RFC 8259). JSON5 does not have an equivalent official standard. It is a community-maintained specification.
JSON5 vs Standard JSON: When to Use Each
Situation Best Choice
--------- -----------
REST API request/response Standard JSON
Data stored in a database Standard JSON
Config file that humans write/edit JSON5 (or YAML)
Any file parsed by JSON.parse() Standard JSON
Any file parsed by a JSON5 library JSON5
VS Code settings or dev tool config JSONC (similar to JSON5)
File shared with external systems Standard JSON
JSON5 vs YAML
Both JSON5 and YAML are designed for human-readable configuration. YAML is more established, supports comments natively, and uses indentation instead of braces. JSON5 keeps the familiar JSON structure while adding convenience features. Choose JSON5 if your team is comfortable with JSON syntax and you want a minimal change. Choose YAML if you prefer a more flexible, indentation-based format.
Summary
JSON5 extends standard JSON with comments, trailing commas, unquoted keys, single-quoted strings, multi-line strings, and special numbers like Infinity. It targets human-written configuration files where strict JSON rules are a hindrance. JSON5 requires a specific parser — standard JSON parsers reject it. Use standard JSON for any data exchanged between systems or parsed programmatically. Use JSON5 only for config files that humans write and that your application explicitly parses with a JSON5 library.
