Need help with your JSON?
Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool
Handling Comments in JSON (Even Though They're Not Supported)
If you are looking for a comment syntax for JSON, the short answer is no: standard JSON does not support comments. A strict parser such as JavaScript's JSON.parse() will reject both // single-line and /* block */ comments. The confusion usually comes from tools that support JSON-like extensions such as JSONC or JSON5.
Short answer
- Standard JSON has no comment syntax.
- Strict parsers reject comments because they are not part of the JSON grammar.
- Comments only work when a specific tool supports an extension such as JSONC or JSON5.
What the JSON standard actually allows
The current JSON standard is RFC 8259. Its grammar does not include comments, which means comments are not valid JSON. The RFC does leave room for permissive parsers to accept extensions, but anything claiming to produce JSON still needs to emit strict JSON.
This is invalid JSON and will fail in strict parsers:
JSON.parse(`{
// Server port
"port": 3000
}`);
// SyntaxError: Unexpected token '/' ...That matters most for API payloads, exported data files, package data, and any file labeled application/json. In those cases, comments are invalid input, not a harmless convenience.
Why comments sometimes seem to work anyway
Some editors and tools intentionally support JSON-like formats for human-edited configuration files. Visual Studio Code, for example, has a jsonc mode for files such as settings.json, tasks.json, and launch.json. In that mode, line comments and block comments are allowed.
- JSONC: Stays close to JSON and adds comments. The recommended extension is
.jsonc. - Current VS Code behavior: Comments work in
jsoncmode, and trailing commas are accepted but discouraged with a warning. - Important limit: A file accepted by one editor or config loader is not automatically valid for
JSON.parse(), APIs, validators, or other languages.
This is the distinction that usually trips people up: a tool can support comments in a JSON-like file, but that does not make comments part of JSON itself.
Choose the right format for the job
1. Use strict JSON when compatibility matters
Use strict JSON when the file is exchanged between systems, sent over an API, checked by generic validators, or consumed by unknown tooling. In those cases, compatibility matters more than authoring convenience.
- Good fit: API requests and responses, exported data, shared config, machine-to-machine interchange.
- Bad fit for comments: anything that eventually goes through a strict JSON parser.
2. Use JSONC for human-edited config when the tool documents support
Use JSONC when comments are part of a local config workflow and the specific tool explicitly supports it. This is common in editor or build-tool configuration, but you should still treat JSONC as a separate format rather than "JSON with a few extras."
- Prefer the
.jsoncextension when you control the file name. - Do not assume another parser will accept the same file just because your editor does.
- Keep trailing commas out unless the tool clearly allows them.
3. Use JSON5 only when you want a looser authoring format
JSON5 supports comments, but it also allows additional syntax such as trailing commas, single-quoted strings, and unquoted object keys. That makes it convenient for humans, but even less appropriate as a drop-in replacement for standard JSON.
{
// This is a single-line comment
name: 'JSON5 Example',
version: '1.0.0',
/* This is a
multi-line comment */
description: 'A configuration file with comments',
enabled: true,
}This is JSON5, not JSON. A strict JSON parser will reject it.
Rule of thumb
If a generic parser, API, or another team needs to read the file, ship strict JSON. Use JSONC or JSON5 only when every consumer explicitly supports that format.
Safe ways to document strict JSON
If you must keep the final file valid JSON, these options are safer than adding comment syntax to the JSON itself.
1. Comment-like properties
{
"_comment": "Main application settings",
"apiEndpoint": "https://api.example.com",
"timeoutSeconds": 30,
"features": {
"_comment": "Feature flags",
"newUi": true
}
}This keeps the file valid JSON, but the comments become real data. Downstream code must ignore them, and strict schemas may forbid them.
2. JSON Schema and adjacent docs
For shared data structures, JSON Schema is often the cleanest replacement for inline comments because it documents fields without polluting the JSON instance itself.
{
"type": "object",
"properties": {
"theme": {
"type": "string",
"description": "UI theme: light, dark, or system"
},
"notifications": {
"type": "boolean",
"description": "Whether the app should send notifications"
}
},
"required": ["theme", "notifications"]
}A README beside the file is also effective when the audience is human rather than machine. This avoids turning documentation into extra JSON fields.
Author with comments, ship without them
When teams want comments during editing but strict JSON in production, the usual pattern is to author in an extended format and convert as part of the build or release process.
- Write the source file in JSONC, JSON5, YAML, or another documented authoring format.
- Validate and convert it before publishing or deploying.
- Emit strict
.jsonfor the final artifact that other systems read. - Test the emitted JSON with the same parser your application uses in production.
Do not remove comments with a naive regex. A pattern like //.* can destroy legitimate strings such as URLs.
{
// Development endpoint
"endpoint": "https://api.example.com/v1",
"timeoutSeconds": 30
}Use a real parser or a dedicated comment-stripping step for the format you chose. Treat comment removal as parsing, not as string replacement.
Common failures and what they mean
- "Unexpected token /": You passed commented content to a strict JSON parser. Remove comments or switch to a parser that explicitly supports JSONC or JSON5.
- "It works in VS Code but not in my app": VS Code may be treating the file as
jsonc, but your runtime probably expects actual JSON. - "Why is there a trailing comma warning?": Comment support and trailing comma support are not the same thing. Some JSONC tooling accepts both, but trailing commas are often discouraged or disabled by default.
- "Can I send JSONC to an API?": Only if that API explicitly documents JSONC support. Otherwise, assume the answer is no.
Need help with your JSON?
Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool