Comments are not allowed in JSON

ErrorJavaScript

SyntaxError: Expected property name or '}' in JSON at position 4 (line 2 column 3)

Where it stops

{
  // how many items
  "count": 1

What it means

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.

Last verified on Chromium 145, Firefox 146, WebKit 26 and Node 20.

It opens with the error marked. Nothing is uploaded. Find where the error is.

Usually
Copying from a JSONC config fileDocumenting a payload before sending itCommenting out a block during debugging
Fix
Remove the comment.

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.

broken.jsonFails to parse
{
  // how many items
  "count": 1
}
JSON.parse stops at line 2, column 3.

Line 2 changes.

fixed.jsonParses
{
  "_comment": "how many items",
  "count": 1
}
JSON.parse reads it.

How the message appears

Chrome and Node first, then Firefox, then Safari. Older versions of Chrome and Node word it differently. Those lines are marked.

Console
  • SyntaxError: Expected property name or '}' in JSON at position 4 (line 2 column 3)Chrome and Node
  • SyntaxError: JSON.parse: expected property name or '}' at line 2 column 3 of the JSON dataFirefox
  • SyntaxError: JSON Parse error: Unrecognized token '/'Safari
  • SyntaxError: Expected ',' or '}' after property value in JSON at position 15 (line 2 column 14)
  • SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 2 column 14 of the JSON data
  • SyntaxError: Expected property name or '}' in JSON at position 4Older V8 wording

What causes it

  1. 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.

  2. Documenting a payload before sending it

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

  3. 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

NoteJSON5 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.

NoteYAML supports # comments and is a superset of JSON, which is why config formats that need comments so often end up as YAML.

What you put into the tools never leaves your browser. We count page views and keep them for 90 days. Accept and we also remember this browser, so a return visit is not counted as new. Privacy policy