Comments are not allowed in JSON

JSON deliberately has no comments. They were removed from the format early on, on the grounds that people would use them to carry parsing directives: so // and /* */ are both syntax errors.

How the message appears

The same fault, worded by different engines. V8 (Chrome, Node, Edge) changed its format around Chrome 109, so older and newer runtimes disagree about the same document.

  • SyntaxError: Unexpected token '/', ..."1 // count"... is not valid JSON
  • SyntaxError: JSON.parse: unexpected character at line 2 column 12 of the JSON data

The fix

Remove the comment. If the note has to travel with the data, put it in a real key (_comment is the usual convention) and have consumers ignore keys beginning with an underscore.

Fails to parse
{
  // how many items
  "count": 1
}
Parses
{
  "_comment": "how many items",
  "count": 1
}

The example travels in the URL fragment, which browsers never send to a server.

What causes it

Copying from a JSONC config file

tsconfig.json, .eslintrc.json and VS Code’s settings.json are JSONC, not JSON. VS Code parses them with a comment-tolerant reader, which is why comments work there and nowhere else.

Documenting a payload before sending it

Annotating an API request body while testing. The comment has to come out before the request goes out.

Commenting out a block during debugging

There is no way to do this in JSON. Delete the block, or move the value under an unused key.

Worth knowing

  • JSON5 and JSONC both support comments. If you control every reader, either is a reasonable choice; if the file crosses a boundary you do not control, it is not.
  • YAML supports # comments and is a superset of JSON, which is why config formats that need comments so often end up as YAML.

Written by Vishnu Shankar.

Your JSON and text are processed entirely in your browser and are never sent anywhere. To understand how the site is used we record each page view (the page, the referrer and an approximate location from your IP address) and keep it for 90 days. Nothing is stored on your device unless you choose it. Accept to also store a device identifier here and record your browser and device characteristics, which is what lets a return visit be told from a new one. Decline and nothing is sent at all. We respect Do Not Track. See our privacy policy.