JSON comments — why they don't exist and what to do instead
JSON has no comment syntax. Here are five practical workarounds, ranked.
·5 min read
Douglas Crockford removed comments from JSON deliberately so people wouldn't put parse directives in them. As a result, // and /* */ are syntax errors.
Workarounds, ranked
- Sibling fields: add a
"_comment"key next to the value. Easy, survives round-trips, but adds noise. - JSONC: a comment-tolerant superset used by VS Code's settings. Requires a JSONC parser to read.
- JSON5: comments + trailing commas + unquoted keys. Great for human-edited config; requires a JSON5 parser.
- YAML or TOML: switch formats if comments matter a lot.
- Strip before parsing: a regex pre-pass that removes comments. Fragile — strings can contain ",
//, " and trip you up.