Comments and trailing commas in JSON
Strict JSON has no comment syntax and rejects a comma after the last element. Several supersets relax this. Here is what each allows, where you meet them, and how to get back to JSON that every parser accepts.
Why JSON has no comments
Douglas Crockford removed comments from the spec deliberately: people were using them to carry parsing directives, which broke interoperability. The cost is that a config format humans edit cannot explain itself. The ecosystem answered with supersets.
The supersets
JSONC — JSON with Comments
Allows // and /* */ comments; most implementations also tolerate trailing commas. Used by VS Code (settings.json), tsconfig.json, .eslintrc.json, and Deno. Strict parsers reject these files.
JSON5
A proper spec: comments, trailing commas, single-quoted strings, unquoted keys, hex numbers, Infinity/NaN, multi-line strings. Used by Babel configs and some Rust/Android tooling. Parsers exist for most languages but are not built in.
HJSON and others
HJSON drops quotes almost entirely. YAML goes further still. Each trades universality for convenience.
Example
{
// service identity
"name": "api",
"replicas": 3, /* scaled manually */
"ports": [80, 443,],
}That is valid JSONC/JSON5 and invalid JSON. Pasted into JSON.parse it fails with Unexpected token /.
Converting to strict JSON
- Online: paste it into the formatter. When strict parsing fails, the lenient parser accepts comments, trailing commas, single quotes and unquoted keys, and emits clean standard JSON.
- Node:
npm i json5thenJSON.stringify(JSON5.parse(text)). For JSONC specifically,jsonc-parser(the library VS Code uses) has astripCommentsfunction. - Python:
pip install json5, orcommentjson. - jq: jq does not accept comments; strip them first with
sed 's#//.*##'(crude — breaks on URLs inside strings).
Should you use them?
For files that humans edit and a specific tool reads — yes, if that tool documents support. For anything that crosses a boundary (API payloads, data exports, files consumed by several programs) — no. Keep the source as JSON5/JSONC and generate strict JSON at build time, the same way you minify.
Trailing commas specifically
They exist to make diffs cleaner: adding a line does not modify the previous line. JavaScript allowed them in ES5, which is why they keep leaking into JSON written by hand. Prettier and most formatters remove them from .json files automatically; turn that on and the problem disappears.