X Xerobit

JSON5 Format — Comments, Trailing Commas, and Extended JSON Syntax

JSON5 extends JSON with JavaScript-style comments, trailing commas, unquoted keys, single-quoted strings, and hexadecimal numbers. Learn when to use JSON5 for config files and...

Mian Ali Khalid · · 4 min read
Use the tool
JSON Formatter
Format, validate, and beautify JSON online. 100% client-side — your data never leaves your browser.
Open JSON Formatter →

JSON5 is a superset of JSON that adds JavaScript-inspired syntax: comments, trailing commas, unquoted keys, and more. It’s widely used for configuration files (tsconfig.json, .eslintrc, VS Code settings).

Validate standard JSON with the JSON Formatter.

JSON5 features vs standard JSON

// JSON5 example — this is NOT valid standard JSON:
{
  // Comments are allowed!
  name: "Alice",           // Unquoted keys
  'nickname': 'Ali',       // Single-quoted strings
  age: 30,
  active: true,
  score: 0xFF,             // Hexadecimal numbers
  ratio: +1.5,             // Leading plus sign
  infinity: Infinity,      // Infinity value
  nothing: NaN,            // NaN value
  multiline: "line 1 \
line 2",                   // Line continuation
  trailing: [
    "item1",
    "item2",               // Trailing comma — OK!
  ],                       // Trailing comma here too
}

JSON5 vs JSONC vs JSON

FeatureJSONJSONCJSON5
Comments✅ (// and /* */)
Trailing commas
Unquoted keys
Single quotes
Hexadecimal
NaN / Infinity
Common inAPIsVS Code, tsconfigBuild configs

JSONC (JSON with Comments) is used by VS Code settings and TypeScript config files — it’s the pragmatic middle ground.

Where you’ll encounter JSON5 / JSONC

  • TypeScript: tsconfig.json allows comments and trailing commas (parsed as JSONC)
  • VS Code: settings.json, keybindings.json, launch.json
  • ESLint: .eslintrc.json (older format)
  • Babel: babel.config.json5 (with json5 package)
  • Webpack: Some webpack.config.json5 setups
  • Renovate Bot: renovate.json5 configuration

Parse JSON5 in Node.js

npm install json5
import JSON5 from 'json5';

const config = JSON5.parse(`{
  // Database settings
  database: {
    host: 'localhost',
    port: 5432,
  },
  maxConnections: 10,
  timeout: 30_000,  // underscore separators not actually in JSON5 spec
}`);

// Stringify to JSON5:
JSON5.stringify(config, null, 2);
// Produces JSON5 format (with trailing commas)

// Stringify to standard JSON:
JSON.stringify(config, null, 2);

Parse JSONC (JSON with Comments) in Node.js

// Simple JSONC stripper:
function parseJSONC(text) {
  const stripped = text
    .replace(/\/\*[\s\S]*?\*\//g, '')   // Block comments
    .replace(/\/\/[^\n\r]*/g, '')        // Line comments
    .replace(/,(\s*[}\]])/g, '$1');      // Trailing commas
  
  return JSON.parse(stripped);
}

// Or use the 'jsonc-parser' package (VS Code uses this):
// npm install jsonc-parser
import { parse } from 'jsonc-parser';

const errors = [];
const result = parse(tsconfigText, errors, { allowTrailingComma: true });
if (errors.length) {
  console.error('JSONC parse errors:', errors);
}

Read tsconfig.json with Node.js

// tsconfig.json is JSONC — strip comments before parsing:
import { readFileSync } from 'fs';
import { parse } from 'jsonc-parser';

function readTsConfig(path = 'tsconfig.json') {
  const text = readFileSync(path, 'utf8');
  const errors = [];
  const config = parse(text, errors);
  if (errors.length) throw new Error(`Invalid tsconfig: ${errors[0].error}`);
  return config;
}

Parse JSON5 in Python

pip install json5
import json5

config = json5.loads("""
{
  // Server config
  host: 'localhost',
  port: 8080,
  debug: true,
}
""")

# config = {'host': 'localhost', 'port': 8080, 'debug': True}

# Load from file:
with open('config.json5') as f:
    settings = json5.load(f)

When to use JSON5 vs standard JSON

Use standard JSON for:

  • REST API payloads
  • Data storage / databases
  • Inter-service communication
  • Any machine-to-machine format

Use JSON5 or JSONC for:

  • Developer-facing config files
  • Tooling configuration (tsconfig, eslintrc)
  • Any file that benefits from comments explaining the settings

Rule: If a human writes it, JSON5/JSONC is friendlier. If a machine generates or reads it, use standard JSON.


Related posts

Related tool

JSON Formatter

Format, validate, and beautify JSON online. 100% client-side — your data never leaves your browser.

Written by Mian Ali Khalid. Part of the Data & Format pillar.