Cookies This site keeps only what it needs to work. There is no analytics and no advertising on it today, and nothing here follows you to other sites. Read the privacy policy

Choose what to allow
+92 312 8299940Free Quote
EN – Language: English
Blog

JSON Comments Why There Are None (and Fixes)

JSON comments don't exist in standard JSON. Here's why, plus every workaround that works: JSONC, JSON5 and comment keys.

By Daniyal Subhani 9 min read

Why Standard JSON Has No Comments

One of the criticisms of JSON by its creator is that comments are often abused to include directives to instruct how the data should be parsed. Ultimately this results in different implementations of the data format, breaking Crockford’s goal of creating a “data format that is both human readable and machine readable”.

JSON Comments
JSON Comments

Thus, in keeping with his original intention to create a lightweight data format that is readable by both humans and machines, the comments had to go.

One reason is practical. Parsers can be very simple and fast if JSON is strict and has no comments. Every parser has to implement its own comment syntax. Different parsers will have different implementations, which is exactly what JSON was created to avoid.

There’s a very strong case to be made against the design decisions implemented within JSON. And if you want to get technical, the definition of a comment within a structured document should actually be a string value within that structured document.

The Workarounds: Json comments

While you may disagree with the decisions made within JSON, in the end a comment is a comment. The good news is, there are a few different ways to do this. Probably one already works for your situation. From the most restrictive to the least restrictive they are:

Comment keys (works in plain JSON)

The simplest trick needs no special tools. Add a regular key whose name signals it's a note.

{
  "_comment": "API key is loaded from the environment, not here",
  "timeout": 30,
  "retries": 3
}

This is valid JSON. Any parser would accept this since _comment is a regular string key. The drawback of using this approach is that your data will be unnecessarily cluttered and your app would have to ignore these keys. Still this is a good approach to add comments to JSON, at least for configuration files.

JSONC (JSON with Comments)

JSONC is JSON plus `//` and `/* */` comments. Microsoft uses it heavily, which is why your VS Code `settings.json` lets you add comments without complaint.

{
  // This runs in development only
  "debug": true,
  "port": 3000 /* default port */
}

JSONC allows the use of single line comments using // as well as multi-line comments using /* */. VS Code and the TypeScript tooling support JSONC. But, other parsers may not. Use JSONC only in files that are edited using VS Code.

JSON5

JSON5 is like JSON, but more liberal. With JSON5, comments, trailing commas, unquoted keys and single quoted values are permitted. JSON5 can be hand written more easily compared to JSON.

{
  // JSON5 is much more relaxed
  name: 'config',      // unquoted key, single quotes
  timeout: 30,         // trailing comma is fine below
}

JSON5 cannot be parsed by standard JSON parsers. As such, a JSON5 specific parser must be used. It can be used to edit config files. But, if the files are to be transmitted over an API, the receiver should only accept standard JSON.

Strip comments before parsing

This is the approach Crockford suggested. Keep a commented file for humans. Run it through a script to strip comments to produce valid JSON for the computer.

This approach is also used in a lot of build tools. You create a config file using a friendly syntax and the tool transforms it to strict syntax before any tool parses it.

The Special Case: package.json

People often ask about package.json, so it is worth explaining here. Comments in package.json will prevent npm from functioning correctly. npm uses a strict JSON parser and so comments will break it.

The common workaround is a comment-style key. Some teams even use an array of strings under a `//` key, which npm tolerates because it's technically valid JSON:

{
  "//": "scripts below are run in CI order",
  "name": "my-app",
  "scripts": {
    "build": "webpack"
  }
}

It’s a hack, but a common one. Just use it sparingly as many // keys in a row are not reliably parsed.

Comparison Table

There’s not much time, so I’ll let you make the choice yourself.

MethodValid in strict JSON?Needs special parser?Best for
Comment key (`_comment`)YesNoQuick notes anywhere
JSONCNoYes (VS Code, TS)Editor config files
JSON5NoYesHand-edited config
Strip before parseYes (output is)A build stepPipelines and tooling
package.json `//` keyYesNoA note in npm config

Mostly speaking, if the file contains data that gets transferred between different systems, use no key or a comment key. If the file contains configuration that gets edited by a user, then using JSONC or JSON5 is preferable.

A Real-World Example

Let’s say you have an app configuration file with a timeout set to 45 seconds to prevent further “optimizations” by other users. You’d probably want to include the reason to explain to users why they should leave it set to 45 seconds.

In JSON you would use the comment key to add the message “45s, raised after the Black Friday outage, do not lower”. In VS Code settings you would put “raised after the Black Friday outage” above the configuration.

The lesson is to use the comment style right to where the config lives, not battle different format styles.

How to Check Your JSON Is Valid

Before you ship a config, validate it. A validator catches the stray comment, the trailing comma. The missing quote that would otherwise crash your app at the worst moment. The free [Denwix JSON tools](here can check and format JSON in your browser.

For the formal spec and the history, [the official JSON site](here lays out the grammar. The [JSON5 project page](here documents its relaxed syntax in full. Both are worth skimming if you work with JSON often.

The Bottom Line

JSON comments don't exist by design. That's not changing. Your options are a comment key for strict files, JSONC or JSON5 for config you hand-edit. A build step that strips comments before parsing. Pick based on who reads the file and what parses it. You'll never fight an "unexpected token" error over a comment again.

Questions and Answers

Can you put comments in JSON?

Not in standard JSON. The spec has no comment syntax. A strict parser rejects both `//` and `/* */`. You have to use a workaround like a comment key, JSONC. @CAP@JSON5.

Why doesn't JSON support comments?

In the opinion of JSON’s creator, comments allowed users to mask parsing directives in JSON, which broken and compromised the spirit of JSON. Comment-less and strict JSON ensures that the data remains the same across all systems.

How do I add a comment in JSON?

A comment key looks like this: “_comment”: “your note here” These keys would look like valid JSON to any parser. Your app would have to look for and ignore them.

What is JSONC?

JSON with Comments, or JSONC, also exists. This format, used by Microsoft to define VS Code configuration files, supports single line and block comments. This variant is not very popular.

What is JSON5?

Finally, JSON has been relaxed even further in JSON5. This format supports single line comments, block comments, trailing values. Unquoted values. Similar to JSON5, this format would also need a custom parser to be useful.

Can I add comments to package.json?

No. Since npm uses a strict JSON parser, comments would cause an error during installation. The workaround is to use a single line comment key, i.e. “//”: “your note”.

What is the difference between JSON and JSONC?

Plain JSON contains no comments. JSONC contains single line and block comments. So while strict JSON has no comments, JSONC does. JSONC comments should not be used with a strict JSON parser.

Does VS Code support comments in JSON?

Yes, in files it treats as JSONC, like `settings.json` and `tsconfig.json`. For files it treats as strict JSON, it will flag comments as errors.

How do I remove comments from JSON?

You should either use a tool that minimizes your configuration file by removing comments, run the configuration file through a JSONC or JSON5 parser. Use a build tool that does this for you.

Will a comment break my JSON?

In a configuration file that uses a strict parser, block comments would result in "Unexpected token" type errors. Removing the comments would resolve the error.

Is a comment key valid JSON?

Yes. A key like "_comment" is just a normal string key with a string value. Every parser accepts it. Your code ignores keys prefixed with an underscore.

Can JSON have // comments?

Not in the standard. Double-slash comments are part of JSONC and JSON5. In plain JSON, comments of any kind are illegal. They should be parsed as errors.

What file extension does JSONC use?

JSONC files often use `.jsonc`, though many tools also accept `.json` files and treat known config files as JSONC. VS Code decides based on the file's language mode.

Should I use JSON5 for my API?

No. Plain JSON should be used to encode data sent over an API, since the receiving party may only accept standard JSON. Otherwise, JSON5 may be used for configuration files that are edited manually.

Why do people use a "//" key in package.json?

Because it is a valid JSON string key. Npm ignores it, it is treated as a comment. This hack, used to add long comments in JS code, should be used sparingly to avoid redundancy.

Can YAML be used instead of JSON for comments?

Yes. YAML uses the hash # to denote a comment. Thus, it is a reliable option to include comments in configuration files. Many tools allow users to give configuration in either JSON or YAML.

Do trailing commas work in JSON?

No. Trailing commas in strict JSON are invalid. JSON5, on the other hand, allows them. Thus, is more convenient to use when editing configuration files by hand.

How do I validate JSON with comments?

Either use a validator which supports JSONC or JSON5. Comment out the code, or remove the comments, and validate the output. Else, plain JSON validators will flag the comments as errors.

Is there a comment syntax coming to standard JSON?

No. The JSON spec intentionally does not allow comments. Thus the workarounds in this document will continue to be the only way to add comments to JSON.

What's the safest way to comment a config file?

If the file must stay strict JSON, use a single comment key. If you control the parser, JSONC or JSON5 gives you real comments. Match the method to the parser that will read the file.

Can I use comments in a JSON config file?

Only if the tool reading it accepts JSONC or JSON5. For strict JSON config, use a comment key instead. Keep a separate file that a build step strips.

What is the safest comment method for shared JSON?

A single comment key, like `"_comment"`, because it's valid in every parser. It travels safely between systems while giving human readers a short note they can see.

Does tsconfig.json allow comments?

Yes, // and block comments work in VS Code and the TypeScript compiler. You can use them in tsconfig.json files. Just be mindful that a strict external JavaScript parser would consider those comments invalid.

Can I nest comment keys in JSON?

Yes. A comment key can sit at any level of the object, next to the values it describes. Keep each note brief and avoid duplicate keys at the same level.

Is JSON5 widely supported?

Use JSON5 in parsers where you control the configuration. Stick to JSON where you control the serialization.

Why does my config break when I add a comment?

If using JSONC or JSON5 is not an option and strict JSON is enforced, remove the comments or switch to a comment key.

Do comment keys affect my program?

Yes, but most apps ignore keys they don’t recognize. A comment key is harmless. Just be sure to document your team’s approach to comments.

Video guide

Video Guide: JSON Comments

This brief video shows each step from start to finish. Choose your language below, and switch on captions if they help.

Try It in Your Browser

Open the free JSON Formatter and do it now, right in your browser.

Prefer Denwix on GoogleReview Denwix on Google