JSON5 Examples

Copy-paste ready code snippets for every JSON5 feature.

Comments

JSON5 supports both single-line and multi-line comments, just like JavaScript.

Single-line Comments

{ // This is a single-line comment name: 'my-app', version: '1.0.0', // Inline comment }

Multi-line Comments

{ /* * Multi-line comment block * Great for longer explanations * or documentation */ config: { /* Inline block comment */ enabled: true, }, }

Practical: Documenting Config Options

{ // Server Configuration // ==================== // The port to listen on (default: 3000) port: 8080, // Enable HTTPS (requires cert files) https: false, /* * CORS settings * - origins: allowed origins (use '*' for all) * - methods: allowed HTTP methods */ cors: { origins: ['https://example.com'], methods: ['GET', 'POST'], }, }

Trailing Commas

Add commas after the last item in arrays and objects. No more syntax errors when reordering!

In Arrays

{ features: [ 'authentication', 'authorization', 'rate-limiting', 'caching', // Trailing comma OK! ], }

In Objects

{ database: { host: 'localhost', port: 5432, name: 'mydb', // Trailing comma OK! }, // Here too! }

Why this matters: Cleaner git diffs! Adding a new item only shows one line changed instead of two.

Unquoted Keys

Skip the quotes on object keys that are valid JavaScript identifiers.

Basic Unquoted Keys

{ name: 'my-project', version: '2.0.0', private: true, main: 'index.js', }

When Quotes Are Still Needed

{ // Unquoted - valid identifiers firstName: 'John', lastName: 'Doe', _private: true, $special: 'allowed', // Quoted - contain special characters 'first-name': 'John', // hyphen 'with spaces': 'value', // spaces '123numeric': 'value', // starts with number }

Strings

JSON5 offers flexible string syntax with single quotes and multi-line support.

Single Quotes

{ // Single quotes - no escaping needed for double quotes message: 'He said "Hello, World!"', // Double quotes - no escaping needed for single quotes contraction: "It's working!", // Mix and match as needed html: '<div class="container">Content</div>', }

Multi-line Strings

{ // Use backslash to continue strings across lines query: 'SELECT id, name, email \ FROM users \ WHERE active = true \ ORDER BY created_at DESC', template: '<html>\ <head><title>Page</title></head>\ <body>Content</body>\ </html>', }

Escape Sequences

{ newline: 'Line 1\nLine 2', tab: 'Column1\tColumn2', backslash: 'C:\\Users\\name', unicode: '\u0048\u0065\u006C\u006C\u006F', // "Hello" }

Numbers

JSON5 supports hexadecimal, special values, and flexible decimal notation.

Hexadecimal Numbers

{ // Colors in hex primaryColor: 0x5AE87E, backgroundColor: 0x1F2937, errorColor: 0xEF4444, // Permissions/flags permissions: 0x755, flags: 0xFF00, }

Infinity and NaN

{ // Special IEEE 754 values maxValue: Infinity, minValue: -Infinity, notANumber: NaN, // Useful for config limits maxFileSize: Infinity, // No limit timeout: Infinity, // Never timeout }

Flexible Decimal Notation

{ // Leading decimal point opacity: .5, scale: .75, // Trailing decimal point version: 2., multiplier: 10., // Explicit positive sign offset: +100, adjustment: +0.5, }

Standard Numbers (Still Work)

{ integer: 42, negative: -17, decimal: 3.14159, scientific: 1.5e10, negativeExp: 2.5e-3, }

Real-World Examples

Complete, production-ready configuration files you can use as starting points.

App Configuration

{ // Application Settings app: { name: 'My Application', version: '2.1.0', env: 'development', }, // Server Configuration server: { host: 'localhost', port: 3000, https: false, }, // Database Connection database: { driver: 'postgres', host: 'localhost', port: 5432, name: 'myapp_dev', pool: { min: 2, max: 10, }, }, // Logging logging: { level: 'debug', format: 'pretty', }, }

Build Configuration

{ // Build Settings build: { outDir: 'dist', sourcemap: true, minify: true, }, // Entry Points entry: { main: './src/index.js', worker: './src/worker.js', }, // Asset Handling assets: { // Files under this size are inlined inlineLimit: 4096, extensions: [ '.png', '.jpg', '.svg', '.woff2', ], }, // Dev Server devServer: { port: 5173, open: true, hot: true, }, }

Test Fixtures

{ // Test Users users: [ { id: 1, email: '[email protected]', role: 'admin', // Full system access }, { id: 2, email: '[email protected]', role: 'user', // Standard user }, { id: 3, email: '[email protected]', role: 'guest', // Read-only access }, ], // Expected test results expected: { totalUsers: 3, adminCount: 1, }, }

Localization (i18n)

{ // English Strings // Translators: Keep placeholders like {name} common: { welcome: 'Welcome, {name}!', logout: 'Log out', save: 'Save changes', cancel: 'Cancel', }, errors: { // Shown when network fails network: 'Connection failed. Please try again.', // Shown for invalid form input validation: 'Please check your input.', // 404 page notFound: 'Page not found.', }, format: { // Date format: Month Day, Year date: 'MMM D, YYYY', time: 'h:mm A', }, }

Ready to Use JSON5?

Learn how to integrate JSON5 into your projects with our step-by-step tutorials.