Can JSON have comments? What JSONC allows and how to clean a commented file

By TechCompare · Updated · How we check

Standard JSON has no comments, so a strict parser rejects any file that has them. The JSON Formatter reads // and /* */ comments when Allow comments is on and leaves them out of the output.

A config file with a // note or a /* */ block often fails with a parse error, even when it looks fine to you. This page explains why, shows what the JSON Formatter does with the comments, and covers what it still rejects. It also says how to get a clean copy for a tool that won't read comments at all.

Topic
JSON with comments
json-comments
Category
Format Comparison
JSON knowledge base

How this is calculated

The rule comes from the grammar. RFC 8259 counts only four characters as insignificant whitespace: space, horizontal tab, line feed and carriage return. A comment has no place in that grammar, so a strict parser stops at the first slash. For a // comment on the second line, Node 24's JSON.parse reports Expected property name or '}' in JSON at position 4 (line 2 column 3). Python's json.loads rejects the same text.

Some tools accept comments anyway. RFC 8259 says, "A JSON parser MAY accept non-JSON forms or extensions." That's why a file can open in one editor and fail in your app. The VS Code JSON docs describe a JSON with Comments (jsonc) mode. VS Code uses it for settings.json, tasks.json and launch.json, and it accepts // and /* */ comments. It also accepts trailing commas, though it warns about them. This formatter makes a stricter call: comments yes, trailing commas no.

Turn on the Allow comments checkbox, and the formatter reads // line comments and /* */ block comments that sit outside strings, then leaves them out of the output. A .jsonc file loads with the option already on. The formatter checks each string before it looks for comments, so the // in the sample's URL stays. A find and replace on // would cut that value short, which is why a tool that reads the string rules is the better choice here. Here's the sample as input:

JSON with comments
{
  // The port the server listens on.
  "port": 8080, /* tcp */
  "url": "https://example.com//api"
}

The formatted output has no comments, because JSON has nowhere to keep them. Here's what the sample gives:

Formatted result
{
  "port": 8080,
  "url": "https://example.com//api"
}

Some errors stay errors. A block comment that never closes gives an error that names the line and column, and a trailing comma still fails, as it does in plain JSON. An easy slip is commenting out the last item. The comma at the end of the item above it now has nothing after it, so delete that comma too. The error points at the closing brace, not at the comma. JSON parse errors covers the other usual mistakes.

When the consumer is strict and you still want notes, pick one of these:

  • Keep the commented file as the source and generate a strict copy from it.
  • Add a _comment key. It's a common habit, not a standard, and a schema that forbids extra keys will reject it.
  • Use YAML when notes matter more than JSON compatibility. YAML supports # comments, and JSON vs YAML compares the two formats.

Verdict

Don't count on the formatter as proof that your app can read the file. Check the strict copy with the parser your app actually uses. Allow comments only covers the main input box, so the second Compare document and the schema box need clean JSON.

More JSON scenarios

Frequently asked questions

Does JSON allow comments?
No. The RFC 8259 grammar has no place for // or /* */, so a strict parser rejects them. The same RFC lets a parser accept extensions, which is why some tools read comments anyway.
What is JSONC?
JSONC is JSON with comments: // line comments and /* */ block comments. VS Code uses it for settings.json, tasks.json and launch.json, and the VS Code JSON docs describe it. It's an extension to JSON, not part of RFC 8259.
Are trailing commas allowed in JSONC?
VS Code accepts them and shows a warning. This formatter doesn't accept them. Allow comments turns on comments only, so a comma after the last item is still an error. JSON parse errors covers the other common syntax mistakes.
How do I remove comments from a JSON file?
Turn on Allow comments and copy the formatted result. The formatter leaves out // and /* */ comments that sit outside strings, and the // in a URL stays. Keep the commented file too, because the copy has no notes in it.
Why does my .json file with comments fail in my app?
A standard parser rejects // and /* */, the same way JSON.parse does in Node 24. Strip the comments before the app reads the file, or use a parser that reads JSONC.