Can JSON have comments?
No. Standard JSON has no comment syntax. The JSON specifications (RFC 8259 and ECMA-404) define only objects, arrays, strings, numbers, booleans, and null, so a // or /* */ comment makes a file invalid JSON. Some tools do accept comments through extensions called JSONC and JSON5, which is why you will see comments in files like tsconfig.json. This page explains what works where, and how to keep notes in JSON without breaking it.
What happens if you add a comment
A strict parser stops at the first comment:
{
"port": 8080 // the public port
}JSON.parse(text) // Node.js / Chrome // SyntaxError: Expected ',' or '}' after property value in JSON at position 17 json.loads(text) # Python # json.decoder.JSONDecodeError: Expecting ',' delimiter: line 2 column 16 (char 17)
The exact message depends on the runtime, but every standards-compliant parser rejects it. You can check any file in the JSON Validator, which points to the exact line and column.
Why JSON has no comments
JSON's creator, Douglas Crockford, removed comments deliberately. Early users were putting parsing directives inside comments, which meant two parsers could read the same file differently. Dropping comments kept JSON a minimal, unambiguous data format that any language can parse the same way. His suggested alternative for config files: write comments if you like, then run the file through a minifier or comment stripper before handing it to a JSON parser.
Which files accept comments?
| File | Comments allowed? | Why |
|---|---|---|
tsconfig.json, jsconfig.json | Yes | TypeScript parses them as JSONC |
VS Code settings.json, launch.json, tasks.json | Yes | VS Code treats them as JSON with Comments |
devcontainer.json | Yes | Dev Containers spec uses JSONC |
.babelrc | Yes | Babel parses it as JSON5 |
package.json | No | npm uses a strict JSON parser |
| API requests and responses | No | Servers and clients use strict JSON |
Any .json read with JSON.parse or json.loads | No | Standard parsers follow the spec |
5 ways to keep comments with JSON
1. Use JSONC where the tool supports it
If the file is read by TypeScript, VS Code, or another JSONC-aware tool, just write comments. In VS Code, files with the .jsonc extension (or the language mode "JSON with Comments") get comment-aware syntax checking.
{
// Strict mode catches most bugs early
"compilerOptions": {
"strict": true,
/* Emit modern JS */
"target": "ES2022"
}
}2. Use JSON5 for your own config
JSON5 extends JSON with comments, trailing commas, single quotes, and unquoted keys. Parse it with the json5 package (npm, and a json5 package on PyPI) instead of the built-in parser.
import JSON5 from "json5"; const config = JSON5.parse(text); // comments and trailing commas OK
3. Add a comment field
The portable trick: store the note as data. It works with every parser, at the cost of the "comment" becoming part of the object.
{
"_comment": "Port must match the load balancer",
"port": 8080
}In package.json the convention is a "//" key, which npm ignores.
4. Strip comments before parsing
Keep comments in the source file and remove them just before the strict parser sees it. In Node, the strip-json-comments package does this. For a one-off, paste the file into Strip Comments: it removes // and /* */ comments (and trailing commas) in your browser and gives you valid JSON.
5. Switch to a format with comments
If people edit the file often and need to explain it, JSON may be the wrong format. YAML and TOML both support # comments natively. See JSON vs YAML for the trade-offs.
Related tools
- Strip Comments: convert JSONC or JSON5 to strict JSON.
- JSON Repair: fix trailing commas, single quotes, and other invalid JSON.
- JSON Validator: find the exact line where a file stops being valid JSON.
- What is JSON?: the rules of the format in plain language.
- Invalid JSON examples: comments and the other mistakes that break parsing.
Frequently asked questions
Can JSON have comments?
No. Standard JSON (RFC 8259 and ECMA-404) has no comment syntax. A // or /* */ comment makes the file invalid, and strict parsers such as JSON.parse and Python's json module reject it. Some tools accept JSON with comments (JSONC or JSON5), but that is an extension, not JSON.
Why doesn't JSON support comments?
Douglas Crockford, who specified JSON, removed comments on purpose: people were using them to hold parsing directives, which broke interoperability between parsers. Keeping JSON comment-free kept it a pure, predictable data format.
How do I add a comment to package.json?
package.json is parsed as strict JSON, so real comments break npm. The common workaround is a "//" key, for example "//": "build runs tsc first". npm leaves keys it doesn't know alone. For longer notes, use the README.
Is JSONC valid JSON?
No. JSONC (JSON with Comments) is JSON plus // and /* */ comments, used by VS Code settings and tsconfig.json. Tools that expect JSONC accept it; a standard JSON parser will throw on the first comment. Strip the comments to turn JSONC into valid JSON.
Does JSON allow trailing commas?
No. A comma after the last item in an object or array is a syntax error in standard JSON. JSONC and JSON5 allow it. The JSON Repair tool removes trailing commas (and other common mistakes) for you.