JSON Comments

Beginner
⏱️ 7 min read
📚 Updated: May 2026
JSON / RFC 8259

What you’ll learn

JSON (JavaScript Object Notation) is a lightweight data‑interchange format that’s easy for humans to read and machines to parse. One thing it deliberately leaves out is a comment syntax. This guide explains why, then walks through the practical workarounds developers use when they need to annotate a configuration file, an API payload, or a fixture.

You’ll see four idiomatic options — sidecar documentation, reserved keys like _comment, a strip‑before‑parse step, and the JSON5 superset — and learn when each is appropriate.

What is JSON?

JSON is a text format that is completely language‑independent but uses conventions familiar to programmers of the C‑family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data‑interchange language for APIs, configuration files, and persistent storage.

Key features of JSON

Simplicity

The grammar fits on a single page — objects, arrays, strings, numbers, booleans, and null.

Lightweight

Compact text with low parsing overhead, perfect for REST APIs and embedded systems.

Language independent

Every mainstream language ships a JSON parser, so the same payload travels safely across stacks.

Human readable

Whitespace is optional but freely allowed, which makes pretty‑printed JSON easy to diff and review.

Why JSON doesn’t support comments

JSON is a data interchange format, not a document language. A valid document encodes exactly one value (usually an object or array) so any compliant parser on any stack can recover the same structure—with no hidden instructions in the syntax.

Douglas Crockford, who popularised JSON, removed comments from the spec after seeing teams abuse them to smuggle parsing directives between tools, which broke interoperability. The fix was to forbid comment tokens entirely. RFC 8259 still requires strict parsers to reject // and /* … */ inside .json files.

How we got here

1

Design goal

Carry data only—no prose or machine directives embedded in the grammar.

Spec
2

What went wrong

Early adopters used comments as a side channel between tools instead of encoding that information as data.

Interop
3

Where we are now

Strict JSON.parse throws SyntaxError. Editors may still accept JSONC or JSON5 for local config files.

Today

Practical check: strict JSON vs your editor

If a .json file has comments and “just works” in the editor, you are using a JSON extension (for example JSONC in VS Code or JSON5). The same bytes will fail JSON.parse in the browser or in many CI pipelines unless you strip comments first.

Workarounds for adding comments

Although JSON does not natively support comments, several patterns are widely used to attach explanatory information to JSON data. Pick the one that matches who reads the file and who parses it.

Option 1

Use a separate documentation file

Keep your JSON pristine and describe its structure in a sibling README.md or schema file. This is the only option that works with any JSON consumer because the data file itself never changes.

  • Best for: public APIs, config files shared between teams, JSON Schema‑driven projects.
  • Trade-off: docs and data can drift out of sync if no review process keeps them aligned.
Option 2

Reserve a key for comments

Add a string‑valued field whose name signals “ignore me”. Conventional choices are _comment, $comment (used by JSON Schema), or //. Every parser accepts them as ordinary data, and consumers that don’t recognise the key simply leave it alone.

JSON
{
  "_comment": "User profile returned by GET /api/users/42",
  "name": "John Doe",
  "age": 30,
  "city": "New York"
}
  • Best for: hand‑edited config files, fixtures, JSON Schema documents.
  • Trade-off: increases payload size and may surface in strict schema validation if the field isn’t declared.
Option 3

Strip comments before parsing

If you control the runtime, write the file with C‑style comments and run a preprocessor (for example the strip-json-comments npm package) before handing the text to JSON.parse. This is exactly the trick VS Code uses for its settings.json.

JSON (with comments to strip)
{
  "name": "John Doe",   // user's full name
  "age": 30,            // age in years
  "city": "New York"    // current city
}
Option 4

Adopt JSON5 (or JSONC)

JSON5 is a superset of JSON that allows comments, trailing commas, unquoted keys, and single‑quoted strings. JSONC (JSON with Comments) is a smaller superset used inside the VS Code ecosystem. Both require a dedicated parser.

JSON5
{
  // unquoted keys, trailing commas, and comments are allowed
  name: "John Doe",
  age: 30,
  city: "New York",
}
  • Best for: developer‑facing tooling, build configs, internal fixtures.
  • Trade-off: not interoperable with strict JSON.parse; never expose JSON5 from a public API.
1

Annotating JSON with reserved keys

A practical pattern for hand‑edited config: prefix any documentation key with an underscore so it sorts together and stays distinct from real fields.

JSON
{
  "_comment1": "This section describes the authenticated user.",
  "user": {
    "name": "John Doe",
    "age": 30
  },
  "_comment2": "Postal address used for shipping.",
  "address": {
    "city": "New York",
    "zip": "10001"
  }
}

Consumers parse the document normally; readers see inline context next to the data it describes.

Key takeaways

1

Strict JSON has no comment syntax — RFC 8259 forbids // and /* … */.

2

For portable annotations, use a reserved key such as _comment or $comment.

3

Use JSON5 or a strip‑before‑parse step only inside trusted tooling, never on public APIs.

Frequently asked questions

No. A standards‑compliant parser will throw a SyntaxError. If a parser accepts comments it is reading a JSON extension such as JSONC or JSON5, not strict JSON.
_comment is a community convention. $comment is reserved by JSON Schema for non‑normative annotations and is therefore safer inside schema documents.
It is strongly discouraged. Most clients call JSON.parse on the response body and will fail on JSON5 features. Reserve JSON5 for files developers edit by hand, and emit strict JSON over the wire.
Install the strip-json-comments package, run the file content through it, then call JSON.parse on the result. VS Code uses the same technique to load settings.json.

Next: JSON.parse()

Now that your JSON is comment‑free, learn how JSON.parse() turns the text into a live JavaScript value — including reviver functions and error handling.

JSON.parse() →
Did you know?

The original JSON specification did include // comments, but Douglas Crockford removed them after seeing teams abuse comments to embed parsing directives. Today RFC 8259 forbids comments entirely — any parser that accepts them is parsing a JSON extension, not JSON.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful