Comments in JSON: why // and /* */ cause errors
JSON has no comment syntax. See the errors comments produce in Chrome, Node.js and Python and the alternatives you can use.
Error messages you may see
| Chrome · Edge · Node.js | SyntaxError: Expected property name or '}' in JSON at position 4 (line 2 column 3) |
|---|---|
| Older Chrome · Node.js 18 and earlier | SyntaxError: Unexpected token / in JSON at position 4 |
| Python json.loads | json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 3 (char 4) |
| This tool | Line 2, Col 3: Comments are not allowed in JSON |
Why it happens
The JSON standard has no comments. Files like tsconfig.json or VS Code settings accept them because those tools use JSONC, a relaxed variant, but JSON.parse and most libraries reject them.
Invalid
{
// user id
"id": 1
}
How to fix
Remove the comments, or store the note as data, e.g. "_comment": "user id". If you need comments in config files, use a format that supports them, such as JSONC, JSON5 or YAML.
Fixed
{
"id": 1
}
Open this example in the JSON formatter →
Tip: the formatter fixes this in one click with the 🔧 Auto-fix button.