Can JSON Have Comments?

You open a config file, add a short note to explain a setting, and save it. Then your program refuses to start with an error like “Unexpected token”. The note is the problem.

Comments feel like they should be allowed everywhere. In JSON they are not. This guide explains why, which JSON-like formats do allow them, and what to do instead.

Quick answer: No. Standard JSON does not allow comments of any kind. Files such as tsconfig.json and VS Code settings use JSONC (“JSON with Comments”), which only some tools accept. To use such a file anywhere else, remove the comments first.

Why doesn’t JSON allow comments?

The JSON standard (RFC 8259) simply has no way to write comments. A parser is the code that reads JSON. A strict parser, like JavaScript’s JSON.parse() or Python’s json.loads(), fails as soon as it sees a comment.

This was done on purpose. Douglas Crockford created JSON. He has said he left comments out for a reason. People were starting to use comments to hold instructions for parsers.

That would have meant different tools could read the same file in different ways. When JSON holds only plain data, it stays the same everywhere.

So this is invalid JSON:

{
  // how long to wait, in seconds
  "timeout": 30,
  /* retry settings */
  "retries": 3
}

What is JSONC?

JSONC means “JSON with Comments”. It is JSON plus two kinds of comments: // for the rest of a line, and /* … */ for a block. Many JSONC tools also accept a trailing comma after the last item.

You have probably used JSONC already. Common examples:

  • VS Code settings files, such as settings.json.
  • tsconfig.json, the TypeScript config file.
  • devcontainer.json, used for development containers.

But there is a problem. These files only work because the tools that read them use a JSONC parser. If you load the same file with JSON.parse(), it fails. Also, not every .json config is JSONC. package.json, for example, must be strict JSON.

What is JSON5?

JSON5 goes further than JSONC. It is a separate format that aims to be easier for people to write. Besides comments and trailing commas, it allows:

  • Keys without quotes.
  • Strings in single quotes.
  • Hexadecimal (base 16) numbers, and numbers like .5, 5. or +1.
  • Infinity and NaN.
  • Strings that continue over several lines.
{
  // JSON5 example
  name: 'my-app',
  port: 0x1F90,
  ratio: .5,
}

Like JSONC, JSON5 needs its own parser. A normal JSON parser rejects all of the above.

Can you use a “_comment” key instead?

A common trick is to put the note in a normal key that your program ignores:

{
  "_comment": "timeout is in seconds",
  "timeout": 30
}

This is valid JSON, and it works when you have no other choice. But it has real downsides:

  • It is data, not a comment. Every program that reads the file sees it, stores it and sends it on.
  • Strict programs may reject it. Some apps and schemas refuse keys they don’t know about.
  • You can’t repeat it. Two "_comment" keys in one object are duplicate keys. Most parsers keep only the last one, without any warning.
  • It doesn’t fit everywhere. You can’t put a key inside an array of numbers or next to a single value.

There are better options. If you control the format, explain it in a README or in a JSON Schema. A JSON Schema has a description field for exactly this. Do people edit the file by hand all the time? Then a format with real comments, such as YAML, may suit you better. See JSON vs YAML.

How to convert JSONC to plain JSON

Sometimes you need a file with comments as strict JSON. For example, you may want to send it to an API or read it with JSON.parse(). To do that, you have to remove the comments and any trailing commas.

Why not just use find and replace?

It is tempting to delete everything after // with a quick search. Don’t. The characters // also appear inside normal strings:

{ "homepage": "https://example.com" }

A simple search would cut that URL in half. A proper converter reads the file the way a parser does. So it knows which // is inside a string and which one starts a comment.

Use a converter

Paste the file into the JSONC to JSON converter. It removes // and /* */ comments and trailing commas, and leaves everything else as you wrote it. You can keep your formatting or indent the output again.

Turn on “Allow JSON5 syntax” to handle JSON5 too. Then it puts quotes around keys, converts single-quoted strings and fixes number formats. Infinity and NaN have no JSON version. So they become null and are listed as changes.

Does the file have other problems too, such as single quotes or Python’s True and None? Then use JSON Repair. It removes comments and fixes those problems as well. It lists every change, so check the list before you trust the result.

Check the result

Finally, paste the output into the JSON Validator. It follows the strict standard, so if it passes there, any JSON parser will accept it. For other errors you might see, read the most common JSON errors.

Common questions

Does package.json allow comments?

No. package.json must be strict JSON, so a comment will break it. Some people use a "//" key as a note, but it is still data with the downsides listed above.

Why does tsconfig.json allow comments?

Because TypeScript reads it with its own forgiving parser, not a strict JSON parser. That is also why another tool reading tsconfig.json with JSON.parse() can fail on it.

What is the difference between JSONC and JSON5?

JSONC is JSON plus comments and, usually, trailing commas. JSON5 also allows unquoted keys, single quotes, extra number formats and multi-line strings.

Will JSON ever support comments?

There is no sign of it. The standard is meant to stay the same. If you need comments, use JSONC, JSON5 or YAML with a parser that supports them.