Comments in JSON: What Works, What Breaks, and Safer Alternatives

Standard JSON has no comment syntax. Learn why parsers reject comments, how JSONC, JSON5 and comment keys differ, and when to strip notes before sharing.

If you want to add a note to a JSON file or temporarily disable a line, the direct answer is that standard JSON has no comment syntax. The JSON grammar defined in RFC 8259 and ECMA-404 does not include // line comments, /* */ block comments or # comments. A strict parser treats any of them as invalid input. Developers still have several workable options, and each one changes either the data or the format. This review explains what each option changes and when each one is safe to use.

Short recommendation

Use comments only when every tool that reads the file explicitly supports a commented dialect. Examples are editor settings files opened in a "JSON with Comments" mode, or a project that parses JSON5 on purpose. For data that is exchanged between systems, sent to an API or read by unknown consumers, keep the JSON plain and put explanations in a separate place, such as a README, a schema description or the code that produces the file. A commented file is easier for people to read, but other tools may not accept it. Plain JSON is the better choice whenever you cannot control or verify every parser that will read the file.

Why comments are not allowed in JSON

JSON was designed as a minimal format for exchanging data. Douglas Crockford, who popularized it, has said he left comments out because people were using them to carry parsing directives, which would have hurt interoperability. Whatever the history, the practical consequence is the same: a conforming JSON parser is not required to accept comments, and most standard-library parsers reject them.

Two examples of this documented behavior: JavaScript's JSON.parse throws a SyntaxError when it reaches a comment, and Python's json module raises a JSONDecodeError. In Visual Studio Code, a file in plain JSON mode shows the warning "Comments are not permitted in JSON." Switching the file's language mode to "JSON with Comments" removes the warning in the editor. It does not change how any other program will parse the file.

What each approach actually changes

The approaches fall into two groups. Some change the format, so the file is no longer standard JSON. Others change the data, so the file stays valid JSON but carries extra values. Neither group is purely cosmetic. Both change the stored characters that every downstream tool will receive.

1. Use a commented dialect (JSONC or JSON5)

JSONC, often called JSON with Comments, adds // and /* */ comments to otherwise ordinary JSON. Tools that accept it include VS Code's settings files and the TypeScript compiler's tsconfig.json handling. JSON5 goes further. Besides both comment styles, it allows trailing commas, single-quoted strings and unquoted keys. Both dialects require a parser built for that dialect.

// JSONC: valid only for parsers that accept comments
{
  "port": 8080,        // development port
  /* "debug": true, */  // temporarily disabled
  "logLevel": "info"
}

This is the only approach that truly lets you comment out JSON lines. The tradeoff is that the file now fails in any strict JSON parser. It is a good fit for configuration files that only one known tool reads. It is a poor fit for API payloads or files you share publicly.

2. Add a comment key inside the data

A common workaround is to store the note as an ordinary property, often named "_comment", "//" or "$comment". The file stays valid JSON and every parser accepts it.

{
  "_comment": "Port must match the load balancer setting",
  "port": 8080,
  "logLevel": "info"
}

Here the note becomes part of the data model. Code that iterates over keys will see it. A schema that forbids unknown properties will reject it. Every consumer that stores or forwards the object will carry it along. Avoid repeating the same comment key within one object. RFC 8259 says object names should be unique, and parsers handle duplicate names inconsistently: some keep the last value, some keep the first, and some report an error. In JSON Schema documents, the "$comment" keyword is defined for notes, so it is a reasonable choice there. Outside schemas, it is only a naming convention.

3. Write with comments, then strip them before use

Some teams keep a commented source file and run a step that removes comments to produce plain JSON for distribution. This keeps the notes where people need them and ships standard JSON to machines. The removal must be done with a parser or a purpose-built comment stripper that understands string literals. A simple text search for // will damage any string value that contains two slashes, such as a web address. Treat the stripped output as the real artifact, and validate it with a strict JSON parser before you rely on it.

4. Switch formats

If comments are essential and you control the readers, a format with native comments may fit better. YAML supports # comments, and YAML 1.2 is designed to accept most JSON. TOML also supports # comments. Changing formats affects every tool in the pipeline, so this is a project-level decision rather than a quick fix.

Comparison of approaches

Approach | Stays valid standard JSON | Can comment out lines | Main risk
JSONC or JSON5 comments | No | Yes | Strict parsers reject the file
Comment key in the data | Yes | No | Note becomes data; schemas or code may reject or misuse it
Strip comments before use | Output yes, source no | Yes, in the source | Naive stripping can corrupt string values
Switch to YAML or TOML | No | Yes | Every reader must support the new format

Will the file still work after copying or sharing?

Copying a commented file between editors preserves the characters, but how the file behaves depends entirely on the parser that reads it next. One tool accepting a JSONC file shows only that this tool tolerates comments. It does not show that a browser, a server framework, a database import or another language's standard library will accept it. Before you share a file, check each destination's documentation, or run a quick test with that destination's own parser.

  • Pasting into an API client or request body: send plain JSON. Comments typically cause a parse error on the server side.
  • Sharing in documentation or chat: commented snippets are fine as illustrations. Label them as JSONC or pseudo-JSON so readers do not paste them into strict parsers.
  • Committing configuration files: confirm which tool reads the file and whether its documentation states comment support.
  • Editing and diffs: comments inside a dialect file are easy to review. Comment keys appear as data changes in diffs and in program output.

Comments do not affect accessibility or reading order the way visual text styling can. A screen reader reading a source file will read comments as ordinary text. The usability question is mainly about parser compatibility, not display.

When plain JSON is the better choice

Choose plain, uncommented JSON when the file crosses a system boundary, when you do not know every consumer, when the data is validated against a strict schema, or when the file is produced and read by programs rather than edited by people. Put the explanation in documentation, in schema descriptions, or in the code that generates the file.

Evidence gaps

This review relies on the published JSON specifications and widely documented parser behavior. It does not include tests of specific tools, versions or platforms. Comment support in editors, build tools and libraries can change between releases, so confirm the behavior in the current documentation of the exact tool you depend on.