TExcelTAD
Home › JSON › JSON5 & JSONC

JSON5 & JSONC

JSON was designed for machines exchanging data, and its strictness is a feature there. But humans also write JSON by hand — mostly configuration files — and for them the rules can feel harsh: no comments to explain a setting, and a trailing comma breaks everything. Two popular supersets relax these rules: JSONC and JSON5.

JSONC: JSON with Comments

JSONC is JSON plus two kinds of comments — // line and /* block */ — and, in most tools, tolerated trailing commas. It isn't a formal standard; it's the dialect used by VS Code settings, tsconfig.json and jsconfig.json, among others.

jsonc
{
  // Editor appearance
  "editor.fontSize": 15,
  "editor.tabSize": 2,

  /* Files the explorer should hide */
  "files.exclude": {
    "**/node_modules": true,
  },
}

That file is fine for VS Code, but a strict JSON parser rejects it:

json · try it yourself
{
  // Editor appearance
  "editor.fontSize": 15
}
Invalid JSON: Expected property name or '}' in JSON at position 4 (line 2 column 3)

Converting JSONC to JSON

To read JSONC with a normal parser you strip the comments first. You can't do that with a naive regex — // might appear inside a string, like a URL. A tiny scanner that tracks whether it's inside a string does the job:

js · try it yourself
function stripJsonComments(text) {
  let out = "";
  let inString = false;
  for (let i = 0; i < text.length; i++) {
    const ch = text[i];
    const next = text[i + 1];
    if (inString) {
      out += ch;
      if (ch === "\\") out += text[++i];          // keep escaped char
      else if (ch === '"') inString = false;
    } else if (ch === '"') {
      inString = true;
      out += ch;
    } else if (ch === "/" && next === "/") {
      while (i < text.length && text[i] !== "\n") i++;
      out += "\n";
    } else if (ch === "/" && next === "*") {
      i += 2;
      while (i < text.length && !(text[i] === "*" && text[i + 1] === "/")) i++;
      i++;
    } else {
      out += ch;
    }
  }
  return out;
}

const jsonc = `{
  // where the API lives
  "apiUrl": "https://api.example.com/v1",  /* note: the // in the URL is kept */
  "retries": 3
}`;

const config = JSON.parse(stripJsonComments(jsonc));
console.log(config);
{ apiUrl: 'https://api.example.com/v1', retries: 3 }

Tip: In real projects use a maintained package such as jsonc-parser (what VS Code uses) or strip-json-comments — they also handle trailing commas.

JSON5: JSON for humans

JSON5 goes further. It allows much of ECMAScript 5's object-literal syntax:

Feature JSON JSON5
Comments // /* */ ❌ ✅
Trailing commas ❌ ✅
Unquoted keys {port: 80} ❌ ✅ (valid identifiers)
Single-quoted strings 'hi' ❌ ✅
Multi-line strings (with \ at line end) ❌ ✅
Hexadecimal 0xFF ❌ ✅
Leading/trailing decimal point .5, 5. ❌ ✅
Explicit plus sign +1 ❌ ✅
Infinity, NaN ❌ ✅

A JSON5 config file:

json5
// app.config.json5
{
  name: 'ExcelTAD demo',
  port: 8080,
  debug: false,
  colors: {
    primary: 0xF0A30A,   // hex number
    opacity: .85,
  },
  motto: 'Learn by \
doing',
}

Since JSON5 is a superset, every valid JSON file is also valid JSON5 — but not the other way round. Here's the same file in strict JSON:

json · try it yourself
{
  "name": "ExcelTAD demo",
  "port": 8080,
  "debug": false,
  "colors": {
    "primary": 15770378,
    "opacity": 0.85
  },
  "motto": "Learn by doing"
}
Valid JSON: object with 5 keys
{
  "name": "ExcelTAD demo",
  "port": 8080,
  "debug": false,
  "colors": {
    "primary": 15770378,
    "opacity": 0.85
  },
  "motto": "Learn by doing"
}

To read JSON5 in JavaScript you need the json5 package (JSON5.parse(text)); browsers and Node don't support it natively.

Which format where?

Use case Recommended
APIs, data exchange between programs JSON — universal, strict, fast
Tool config edited in VS Code (tsconfig.json, settings) JSONC — whatever the tool expects
Hand-written config where your app controls the parser JSON5, YAML or TOML
Logs and streams JSON Lines

Note: A file named .json might still be JSONC — tsconfig.json allows comments even though its extension says JSON. Always check what the tool's parser accepts.

Keeping "comments" in strict JSON

If you must stay with strict JSON, a common convention is a dedicated key that programs ignore:

json · try it yourself
{
  "_comment": "Retries are capped at 5 by the server",
  "retries": 3
}
Valid JSON: object with 2 keys
{
  "_comment": "Retries are capped at 5 by the server",
  "retries": 3
}

Test yourself

Which of these is allowed in JSONC but not in JSON?

  1. Double-quoted strings
  2. // comments
  3. Nested arrays
  4. null

Is every valid JSON document also valid JSON5?

  1. Yes, JSON5 is a superset of JSON
  2. No, JSON5 forbids double quotes
  3. Only if it has no arrays
  4. Only if it contains comments

Why is stripping comments with a simple regex like /\/\/.*$/gm risky?

  1. Regexes are slow
  2. It would also cut // inside strings such as URLs
  3. It removes line breaks
  4. It changes numbers