JSON Errors / All languages

Comments in JSON: Unexpected token /

How to Fix: Comments in JSON: Unexpected token /

Standard JSON has no comment syntax. A // line comment or /* block comment */ anywhere in the document makes parsers fail with "Unexpected token /" (JavaScript) or "Expecting value" (Python) at the comment's position.

Comments were deliberately excluded from the JSON specification, partly to prevent them being abused for parsing directives that would fragment the format.

{"find": "error"}paste your JSON below

Common causes

1. Documenting a config file with // comments

Natural instinct from every programming language - but it breaks the file for standard parsers.

Invalid
{
  // server port
  "port": 8080
}
Valid
{
  "port": 8080
}

2. Copying from JSONC files (VS Code settings, tsconfig)

VS Code's settings.json and tsconfig.json are JSONC - a variant that allows comments. Copying their content into real JSON contexts carries the comments along.

How to fix it

Use a data field for documentation
{
  "_comment": "Server port - keep in sync with the load balancer",
  "port": 8080
}
Or use a format that supports comments
# YAML supports comments natively
port: 8080  # keep in sync with the load balancer

// JSON5 (needs a json5 parser library)
{
  // keep in sync with the load balancer
  port: 8080,
}

Frequently asked questions

Why were comments removed from JSON?

Douglas Crockford removed them early in JSON's history after seeing them used to hold parsing directives, which threatened interoperability - and to keep the grammar minimal. Data-only, no annotations, every parser agrees.

Is the "_comment" key convention safe?

Yes - it is just a regular key that consuming code ignores. Downsides: it travels with the data (larger payloads) and only attaches per-object, not per-line. For heavily documented configs, YAML, TOML, or JSONC are better fits.

How do I strip comments from a JSONC file programmatically?

Use a JSONC-aware parser (like the jsonc-parser npm package or strip-json-comments) rather than a regex - naive regexes corrupt strings that contain // or /* sequences, like URLs.

Last updated:

Related JSON errors

You might also need

Free and in-browser, like everything on JSON Console. Browse all tools →