JSON Has No Comments: What to Do Instead (JSONC, JSON5, and Keys)
Standard JSON has no comment syntax, so a // or /* */ line makes the file invalid. If you need comments, use JSONC or JSON5 where the tool reading the file supports them, or keep your notes somewhere else.
Quick answer
The reason json comments are not allowed is simple: the grammar has nowhere to put one. You have four realistic choices:
- JSONC (JSON with Comments), when an editor or tool says it reads that dialect.
- JSON5, when the program has a JSON5 parser.
- Strict JSON plus notes elsewhere, when the file crosses between systems.
- A `_comment` key, as a last resort, because it turns a note into data.
Each one has a cost, and the rest of this article covers them.
How it actually works
What the grammar allows
RFC 8259 defines the format. Between values it allows only "insignificant whitespace": space, tab, line feed and carriage return. The grammar looks like this:
ws = *( %x20 / %x09 / %x0A / %x0D )
JSON-text = ws value wsThere is no production for a comment, so there is nothing for // note to match. The RFC never says "no comments" in so many words; the grammar simply leaves them out.
There's a loophole, and it explains why some tools accept comments anyway. The RFC says a parser MUST accept every text that conforms to the grammar, and that it "MAY accept non-JSON forms or extensions." Generators, on the other hand, must produce text that strictly conforms. So a parser that tolerates comments is allowed, but a file with comments in it is not JSON. Don't assume the next program in the chain is as forgiving. You can check a file against a strict parser from the terminal with a validate-JSON pass.
JSONC: comments when the tool expects them
VS Code has a "JSON with Comments" mode, called jsonc. Per the VS Code docs, it is used for its own configuration files such as settings.json, tasks.json and launch.json. In that mode you can write single-line // comments and block /* */ comments, as in JavaScript.
{
// Show line numbers in the gutter
"editor.lineNumbers": "on",
/* Wrap long lines */
"editor.wordWrap": "on"
}The mode also accepts trailing commas, but the docs discourage them and the editor shows a warning. You can associate other file names or patterns with jsonc through the files.associations setting. That changes how the editor reads the file. Other programs still read it their own way.
JSON5: a superset for hand-written files
JSON5 describes itself as "an extension to the popular JSON file format that aims to be easier to write and maintain by hand (e.g. for config files)." It is a superset of JSON, so valid JSON is always valid JSON5. On top of comments it adds a few conveniences:
{
// comments are allowed
name: 'demo', // unquoted key, single-quoted string
retries: 3,
tags: ['a', 'b',], // trailing comma
}The same page lists unquoted object keys, single-quoted strings, hexadecimal numbers and a single trailing comma in objects and arrays. The caveat sits on the same page: it "is not intended to be used for machine-to-machine communication." Use it when a person edits the file and a JSON5 parser reads it.
The _comment key and the repeated-key trap
The workaround you will see most is a key that exists only to hold a note:
{
"_comment": "Timeout is in seconds",
"timeout": 30
}The file stays valid, but the note is now data. The program reading it may see it, reject it as an unknown key, or pass it along. Nothing in the format marks it as a comment.
Reusing one key for several notes is worse. RFC 8259 says names within an object SHOULD be unique, and warns that when they are not, "the behavior of software that receives such an object is unpredictable." Some implementations keep only the last pair, others fail, and some report every pair. A file that works today can behave differently in the next tool.
One more case: VS Code's $schema key is, in the docs' words, "specific to VS Code and not part of the JSON Schema specification," and adding it "changes the JSON itself, which systems consuming the JSON might not expect." Any extra key has that cost.
When to use it (and when to skip it)
Pick by who reads the file, not by which syntax you like:
- Only an editor like VS Code reads it (its own settings, for example): JSONC is the natural fit.
- A program with a JSON5 parser reads it, and a person edits it: JSON5 gives you comments and fewer quoting chores.
- The file is an API payload, or it moves between systems: stay with strict JSON. Put the explanation in a README next to the file or in the code that reads it.
- You want a lot of commentary in the file itself: consider a different format. JSON, YAML or TOML compares what you give up in each, and the `.env` file is another format whose comment rules are loose.
Every dialect has a trade-off: fewer tools can read the file. A comment you can type but your build tool refuses isn't a feature.
How Carets fits in
Carets is a fast, native notes and code editor for iPhone, iPad and Mac. It keeps Markdown, plain text and code in one place, with syntax highlighting for code and config files.
That suits this problem. Strict JSON can't hold notes, so a practical habit is a plain-text notes file beside the config, such as config.notes.md, that says why each value is what it is. In Carets both files sit in the same project, and your files stay plain text, so they stay portable. For a fuller look at the workflow, see editing config files and dotfiles on Mac and iOS.
Get Carets on the App Store for iPhone, iPad and Mac.
Frequently Asked Questions
Does JSON allow comments?
No. The RFC 8259 grammar allows only space, tab, line feed and carriage return as insignificant whitespace and defines no comment syntax. A strict parser rejects // and /* */.
Is JSONC the same as JSON?
No. JSONC is JSON plus // and /* */ comments, used by editors such as VS Code for files like settings.json. A file with comments is not valid JSON, so only tools that expect JSONC should read it.
Can I use a duplicate key as a comment?
Avoid it. RFC 8259 says names SHOULD be unique and that behavior with duplicates is unpredictable: some parsers keep the last pair, some fail, and some report all of them.
Should I use JSON5 for a config file?
Only if the program reading it has a JSON5 parser. JSON5 is a superset of JSON aimed at hand-written config, and it is not meant for machine-to-machine exchange.