Introduction
JSON5 is a superset of JSON designed to make configuration files easier for humans to write and maintain. It adds features that developers have wanted in JSON for years: comments, trailing commas, unquoted keys, and more. If you've ever been frustrated by JSON's strict syntax, JSON5 is the answer.
JSON vs JSON5: Side by Side
Standard JSON
{
"name": "My App",
"version": "1.0.0",
"database": {
"host": "localhost",
"port": 5432
},
"features": [
"auth",
"logging"
]
}JSON5
{
// App configuration
name: 'My App',
version: '1.0.0',
database: {
host: 'localhost',
port: 5432,
},
features: [
'auth',
'logging',
],
}JSON5 Features
1. Comments
The most requested JSON feature. Add // line comments and /* */ block comments:
{
// This is a line comment
name: 'My App',
/* This is a
block comment */
version: '1.0.0',
}2. Trailing Commas
No more syntax errors when you add an item to the end of an array or object:
{
features: [
'auth', // Add new items after
'logging', // without removing commas
'cache', // from previous lines
],
}3. Unquoted Keys
Keys don't need quotes if they're valid JavaScript identifiers:
{
name: 'Alice', // No quotes needed
age: 30,
'special-key': true, // Quotes for special chars
}4. Single-Quoted Strings
Use single quotes instead of double quotes:
{
greeting: 'Hello, World!',
path: 'C:\Users\name', // Less escaping
}5. Hexadecimal Numbers
{
maxMemory: 0x1000, // 4096 in decimal
flags: 0xFF, // 255 in decimal
}6. Multi-line Strings
{
description: 'This is a \
multi-line string',
}Where JSON5 is Used
- Babel -
.babelrcandbabel.config.jsonsupport JSON5 - TypeScript -
tsconfig.jsonsupports comments (a JSON5 feature) - ESLint -
.eslintrcfiles can use JSON5 syntax - Prettier -
.prettierrcsupports JSON5 - Renovate -
renovate.json5configuration
Using JSON5 in Your Project
// Install the json5 package
// npm install json5
// Reading JSON5
const JSON5 = require('json5');
const config = JSON5.parse(fs.readFileSync('config.json5', 'utf8'));
// Writing JSON5
const output = JSON5.stringify(config, null, 2);
fs.writeFileSync('output.json5', output);
// In ESM
import JSON5 from 'json5';When Not to Use JSON5
- API responses - Use standard JSON for maximum compatibility
- Data exchange - Not all languages have JSON5 parsers
- High-throughput parsing - JSON5 parsing is slower than JSON
- Browser-native parsing -
JSON.parse()doesn't support JSON5