Use JSON with comments in Visual Studio Code

How VS Code's JSON with Comments (jsonc) mode works, what it changes in your files, and when to keep plain JSON instead.

Visual Studio Code can open a file in a language mode called JSON with Comments, whose language identifier is jsonc. In this mode the editor accepts comments inside JSON, so you can annotate configuration values without seeing errors. Whether that helps or causes problems depends on the program that reads the file afterward.

Short verdict: is JSON with comments right for your file?

JSON with comments works well for configuration files whose consuming tool is documented to accept comments. Examples include VS Code's own settings, launch and task files, and TypeScript or JavaScript project configuration. The main benefit is that explanations sit next to the values they describe. The deciding tradeoff is that the file is no longer standard JSON, and a strict parser will reject it. Keep plain JSON for request bodies sent to APIs, package manifests, files read by strict standard-library parsers, and anything you share with tools or people whose parser you don't know.

What the jsonc mode changes and what it leaves alone

Two separate actions are involved. Choosing a language mode changes only how the editor validates, highlights and formats the file; it does not alter the bytes on disk. Writing comments does change the stored content. The file then contains comment text that the JSON specification (RFC 8259) does not permit. No visual effect or character substitution is involved: the comments are ordinary text characters saved in the file.

  • Line comments starting with // are accepted.
  • Block comments enclosed in /* and */ are accepted.
  • Format Document reformats the structure and keeps the comments in place.
  • VS Code's documentation describes trailing commas in this mode as accepted but discouraged, with the editor showing a warning. Confirm this behavior in your installed version, because no version-specific test was supplied for this article.
{
  // Increase only if large files are slow to load
  "maxFileSizeMB": 50,

  /* Paths excluded from indexing.
     Keep build output out of search results. */
  "exclude": ["dist", "node_modules"]
}

How to turn on JSON with Comments in VS Code

  • For one open file: click the language indicator in the status bar, choose Select Language Mode, then pick JSON with Comments. This affects only the current editor session for that file.
  • By file extension: name the file with a .jsonc extension so the mode applies automatically.
  • By pattern: add a files.associations entry in your settings that maps a filename pattern to jsonc, as shown below.
{
  "files.associations": {
    "*.config.json": "jsonc"
  }
}

VS Code already opens several of its own configuration files in this mode, such as settings.json, launch.json and tasks.json. Defaults for other files can depend on your version and installed extensions. To check a particular file, look at the language name shown in the status bar. A comment in a file opened in plain JSON mode is flagged as an error. That error means the comment is invalid JSON; it does not mean the editor is misbehaving.

Will commented JSON still work after copying or sharing?

Pasting commented JSON into another file or tool that accepts jsonc keeps it working. Pasting it into a destination that expects strict JSON usually causes a parse error. Common examples are an API request body, a web form validator, or code that calls JavaScript's JSON.parse or Python's json module, both of which reject comments. Editor mode does not travel with copied text, so the receiving tool applies its own rules.

Some JSON files are read by tools that never accept comments, even when VS Code would display them without complaint. A package.json read by npm is a common example: comments there break installs and scripts. Before adding comments to any file, check what the consuming tool's documentation says about comment support.

Searching and editing are mostly unaffected. Comments are plain text, so text search finds them and they move with the surrounding lines when you edit. JSON-aware command-line tools and validators that expect strict JSON may fail on the file or require a step that strips the comments first.

On accessibility, comments are ordinary characters in normal reading order, so assistive technology that reads the file as text should encounter them where they appear. No scoped test with a named screen reader was supplied for this article, so that expectation is unverified for any specific product. Long comment blocks also add material that a keyboard or screen reader user has to move through. Keep comments short and close to the values they explain.

When you need a plain JSON version

Produce a comment-free copy whenever the file goes to a strict parser, an external service, or a recipient whose tooling you can't confirm. Removing comments is a real edit, not a display change. Afterward, also remove any trailing commas, then switch the file to plain JSON mode or run it through a strict validator to confirm that it parses. If notes must travel with the data, a separate README is safer than a placeholder key such as _comment. A placeholder key adds data that the consumer may read, store or reject.

Gaps in the available evidence

This overview rests on documented, general behavior of the JSON format and the VS Code editor. It does not rest on measurements or tests supplied for this article. Default file associations, trailing-comma handling and extension behavior can differ between VS Code versions and setups. Comment support in third-party tools varies by tool. Check your installed version and each consumer's documentation before relying on comments in a shared or production file.