JSON vs JSON5

A complete side-by-side comparison of syntax, features, and use cases.

TL;DR: JSON5 is a superset of JSON. Every valid JSON file is valid JSON5. JSON5 adds human-friendly features while maintaining full backward compatibility.

Syntax Comparison

A comprehensive comparison of what each format supports.

Feature JSON JSON5
Comments Not allowed // and /* */
Trailing commas Syntax error Allowed
Unquoted keys Must quote Valid identifiers
Single quotes Double only Single or double
Multi-line strings Not allowed With \ escape
Hexadecimal numbers Not allowed 0xFF format
Infinity/NaN Not allowed Allowed
Leading decimals Must be 0.5 .5 allowed
Trailing decimals Must be 5.0 5. allowed
Positive sign Not allowed +10 allowed

Code Examples

Configuration File

A typical app configuration showing the readability difference.

JSON

{ "database": { "host": "localhost", "port": 5432, "ssl": true }, "cache": { "ttl": 3600, "maxSize": 100 } }

JSON5

{ // Database connection settings database: { host: 'localhost', port: 5432, ssl: true, // Required for prod }, // Cache configuration (seconds) cache: { ttl: 3600, // 1 hour maxSize: 100, // MB }, }

Package Manifest

Managing dependencies with documentation.

JSON

{ "name": "my-project", "version": "1.0.0", "dependencies": { "lodash": "^4.17.21", "express": "^4.18.0" } }

JSON5

{ name: 'my-project', version: '1.0.0', dependencies: { // Utility library - used for deep merge lodash: '^4.17.21', // Web framework - handles routing express: '^4.18.0', }, }

Test Data Fixtures

Test data with explanatory comments.

JSON

{ "users": [ { "id": 1, "name": "Alice", "role": "admin" }, { "id": 2, "name": "Bob", "role": "user" } ] }

JSON5

{ users: [ { id: 1, name: 'Alice', role: 'admin', // Full access }, { id: 2, name: 'Bob', role: 'user', // Read only }, ], }

Feature Matrix

Detailed breakdown of capabilities and compatibility.

Category Feature JSON JSON5 Notes
Comments Single-line (//) C-style comments
Multi-line (/* */) Block comments
Strings Double quotes Standard strings
Single quotes Reduces escaping
Multi-line Using \ at line end
Numbers Integers/Decimals Standard numbers
Hexadecimal 0xFF format
Infinity/NaN IEEE 754 values
Leading/trailing decimal .5 or 5. syntax
Objects Quoted keys Required in JSON
Unquoted keys Valid identifiers only
Commas Trailing commas Arrays and objects

When to Use Each Format

Use JSON When...

  • API communication - Machine-to-machine data exchange where strict parsing is required
  • Database storage - Structured data that won't be manually edited
  • Web APIs - REST/GraphQL responses and payloads
  • Security-critical data - When deterministic parsing matters
  • Third-party integration - When the consumer expects strict JSON

Use JSON5 When...

  • Configuration files - Settings that humans read and edit regularly
  • Test fixtures - Mock data that benefits from inline documentation
  • i18n files - Translations with translator notes and context
  • Design tokens - Colors, spacing, typography with explanations
  • Any hand-edited data - Files maintained by developers

Migrating from JSON to JSON5

Migration is effortless because JSON5 is a superset of JSON.

1

Rename file extension (optional)

Change .json to .json5 for clarity. Many tools support both.

2

Update your parser

Replace JSON.parse() with the JSON5 library: npm install json5

// Before const data = JSON.parse(text); // After import JSON5 from 'json5'; const data = JSON5.parse(text);
3

Gradually adopt features

Your existing JSON is already valid. Add comments, trailing commas, and other features as needed.

4

Configure your editor

Add JSON5 syntax highlighting for .json5 files. Most editors have extensions available.

Pro tip: Start with configuration files in your project. They benefit most from comments and are low-risk to migrate.

Compatibility Notes

Important considerations when choosing between formats.

Interoperability

JSON5 can parse all JSON, but JSON cannot parse JSON5 features. If your data will be consumed by a strict JSON parser, output in JSON format using JSON.stringify().

Performance

JSON parsing is slightly faster since it's native to JavaScript. JSON5 parsing requires a library. For config files loaded once at startup, this difference is negligible.

Bundle Size

The JSON5 library adds ~5KB minified to your bundle. For server-side applications or build tools, this is rarely a concern.

Security

Both formats are equally safe for data interchange. JSON5 does not execute code - it's purely a data format like JSON.

Ready to Learn More?

Explore practical examples or dive into comprehensive tutorials.