Can JSON Have Comments? JSONC, JSON5 and Safe Alternatives
Short answer: no. Standard JSON has no comment syntax, and JSON.parse, Python’s json, jq and almost every other parser reject // and /* */. Yet tsconfig.json and VS Code settings are full of comments. This guide explains why, what “JSONC” and “JSON5” are, and the safe ways to document a JSON file.
What happens when you add a comment
{
// port for the dev server
"port": 3000,
/* allowed origins */
"origins": ["http://localhost:3000"],
}Real error messages for that file:
JavaScript: Expected property name or '}' in JSON at position 4 (line 2 column 3)
Python: Expecting property name enclosed in double quotes: line 2 column 3 (char 4)
jq: parse error: Invalid numeric literal at line 2, column 5None of them mention comments – they just stop at the first /. If you get one of these errors on a config file, look for comments and trailing commas first (the file above has both).
Why JSON has no comments
JSON’s creator, Douglas Crockford, left comments out on purpose. He saw people using comments to hold parsing instructions, which would have broken compatibility between parsers. Keeping JSON minimal is a big reason why every language can read it the same way.
JSONC: JSON with Comments
Some tools accept a relaxed variant usually called JSONC: regular JSON plus // and /* */ comments, and often trailing commas. Files that commonly allow it:
tsconfig.jsonandjsconfig.json(TypeScript)- VS Code
settings.json,launch.json,tasks.json,devcontainer.json - Files ending in
.jsonc
package.json does not allow comments – npm will fail to read it. And if your own code reads tsconfig.json with JSON.parse, it breaks on the first comment.JSON5: a friendlier superset
JSON5 goes further: comments, trailing commas, unquoted keys, single-quoted strings, hex numbers, Infinity and more. You need the json5 library to read it:
import JSON5 from 'json5' // npm install json5
JSON5.parse(fileText)
// { port: 3000, origins: [ 'http://localhost:3000' ] }
JSON5.parse("{ unquoted: 'single', hex: 0xFF, trailing: [1, 2,], plus: +1, inf: Infinity }")
// { unquoted: 'single', hex: 255, trailing: [ 1, 2 ], plus: 1, inf: Infinity }JSON5 is good for config files that humans edit. Don’t use it for data exchanged between systems or APIs – the other side probably expects strict JSON.
Reading a file that has comments
- JavaScript/TypeScript: use
json5, or a JSONC parser such asjsonc-parser(the one VS Code uses). - Python:
pip install json5, thenjson5.load(f). - One-off cleanup: paste the file into JSON Repair. It removes comments and trailing commas and gives you strict JSON.
text.replace(/\/\/.*$/gm, '') also deletes everything after the // in "http://example.com", which corrupts the string. In our test it produced Bad control character in string literal in JSON at position 15. A correct stripper must skip over strings – use a library.Documenting strict JSON without comments
When the file must stay standard JSON:
- A comment field that your program ignores:
Remove it before use if needed:{ "_comment": "port for the dev server", "port": 3000 }jq 'del(._comment)'. - Self-explanatory key names:
"requestTimeoutSeconds": 30needs no comment;"timeout": 30does. - A JSON Schema with
descriptionfields – editors like VS Code show them as tooltips and autocomplete. See the JSON Schema tutorial. - A README next to the file for longer explanations.
- Switch format if humans edit the file a lot: YAML and TOML support comments natively. See JSON vs YAML.