JSON is famous for what it includes — strings, numbers, objects, arrays, and very little else. It is equally famous, among people who edit configuration files for a living, for what it leaves out. Two of the most beloved conveniences from other languages and formats are absent: comments and trailing commas. You cannot write // or /* */ anywhere in a JSON document, and you cannot place a comma after the final member of an object or the final element of an array. Both are hard parse errors in strict parsers, and both fail for the same underlying reason: the JSON grammar was designed to be as small as a data exchange format allows, and every addition was weighed and deliberately rejected.
This guide covers the grammar facts, the designer's stated reasoning, where the annotating habit comes from, the alternatives that keep configurations readable without breaking the parser, and a recovery workflow for documents that arrive polluted with comments and stray commas.
What the grammar actually says
RFC 8259, the current specification for JSON, defines the entire language in a handful of productions. There is no production for a comment, and there is no production that permits a comma immediately before a closing brace or bracket. Whitespace is allowed between tokens; that is the full extent of the latitude a strict parser gives you.
Put a comment anywhere and a conforming parser stops:
{
// Retry with backoff before giving up
"retries": 3,
/* timeout is in milliseconds */
"timeoutMs": 2500
}
The parser reads the opening brace, reads "retries", and then encounters the character /, which cannot begin any JSON token. Every strict parser reports an error at that point. Python's json module raises JSONDecodeError, Go's encoding/json returns an invalid character fault, browsers' JSON.parse throws a SyntaxError, and Rust's serde_json reports an unexpected token. None of them offer a lenient mode or a flag to enable comments. The grammar is the API.
The trailing comma fails the same way:
{
"retries": 3,
"timeoutMs": 2500,
}
After 2500 the parser sees a comma, so it expects another member — a string, then a colon, then a value. Instead it sees }, which cannot start a member. The wording of the error varies by implementation, but the verdict does not: a comma before a closing brace is never valid JSON. The same holds for arrays. [1, 2, 3,] is a parse error, while [1, 2, 3] is not.
These two rules surprise people because the ecosystems around JSON are more forgiving. That asymmetry is examined below, but the baseline is worth stating plainly: strict JSON has no comment syntax and no trailing comma, and every attempt to use either is a parse error, not a warning. A file that contains one will not load, will not deploy, and will not be read — the parser's refusal is total.
Why the designer left them out
JSON was not designed as a configuration format. It was extracted from JavaScript object literals in the early 2000s as a data exchange format — a way for programs written in different languages to ship structured data to each other over the network. Douglas Crockford, who specified and promoted the format, made a deliberate decision to keep only what a data format needs and to strip everything else, including features JavaScript itself has.
Comments were the most contested omission, and Crockford has explained the reasoning many times. The argument runs in three parts.
First, a data format is read by programs, not by people. When one system sends data to another, the recipient is a parser, and a parser that must skip arbitrary prose has more surface area for bugs and for divergent behavior between implementations. Keeping the grammar minimal was a bet that a format almost anyone could implement correctly in an afternoon was worth more than a format convenient to annotate. The history of the web is full of formats that lost that bet.
Second, comments invite executable content. In the formats JSON displaced, comments frequently carried directives — preprocessor instructions, pragma lines, annotations that a downstream tool was expected to notice and act on. Crockford's stated concern was that once comments exist, people start putting information in them that the data consumer is supposed to interpret, and implementations begin to disagree about what a comment means. Remove comments entirely and that entire class of disagreement disappears. A comment-free grammar cannot carry hidden instructions.
Third, annotations belong outside the data. Crockford's recommendation, when asked how to document a JSON file, was consistent: use external documentation, or use a designated member such as a "comment" or "_comment" name if the annotation must travel with the data. A member is real data — every parser handles it identically, it survives every round trip, and consumers that do not care about it can ignore it by name.
{
"_comment": "Retries apply to the billing API only; timeout is in milliseconds.",
"service": "billing",
"retries": 3,
"timeoutMs": 2500
}
The trailing comma had a simpler justification: it is a JavaScript quirk, not a data requirement. Trailing commas are legal in JavaScript array and object literals, where they exist mainly to make mechanical list building and reordering less error-prone. When JSON was carved out of that syntax, the trailing comma was classified as syntax noise that no interchange scenario needed, and it was dropped. The result is a grammar in which every comma separates two things that both exist — a property that keeps parsers trivial and makes byte-level comparisons of equivalent documents meaningful.
Whether or not you agree with the trade-off, it was a trade-off, made consciously, and every strict parser in the world implements it the same way. The grammar has not changed since: through RFC 4627, RFC 7159, and RFC 8259, neither comments nor trailing commas ever gained a place.
Where the habit comes from
Nobody writes // into a JSON file because they guessed. The habit is imported from two places, and naming them changes how you fix the problem.
The first is JavaScript itself. Object literals in JS accept comments and trailing commas, and developers who spend their days in .js files carry the muscle memory into .json files, which look nearly identical but parse under stricter rules. The confusion is compounded by tools such as package.json: some package managers tolerate constructs that the spec does not, which teaches people that JSON strictness is a matter of configuration. For plain JSON data, it is not. The file either conforms or it does not parse.
The second is configuration formats that do allow comments. YAML, TOML, HJSON, and JSON5 all support comments; several also permit trailing commas, unquoted keys, or other conveniences. Teams that migrate a configuration file from YAML to JSON — usually for tooling or ecosystem reasons — lose annotation ability and instinctively reach for the syntax they were using the week before. The same migration explains the single-quote problem: habits from permissive formats are the leading source of strict-JSON parse errors, and comments are merely the most visible of them.
The pattern is worth naming because it changes the remedy. These errors are not carelessness; they are cross-format interference. The person who wrote the comment into the JSON file was doing the correct thing for YAML. The durable fix is either to give them an annotation mechanism that works in JSON, or to keep the file in a format that supports what they actually need. Scolding people for writing comments is not a fix; a convention is.
The annotation problem, and alternatives that work
Comments in a config file serve a real purpose. They answer "why is this value what it is" for the next person who opens the file — who is very often the author six months later with no memory of the reasoning. JSON's rejection of comments does not make that need disappear; it relocates the answer. Four approaches cover essentially every situation.
Designated members
The "_comment" pattern shown above is the lightest-weight option and the one Crockford endorsed. Variants are common: a "notes" member, a "//" member, or a "_meta" object that gathers all annotations in one place so consumers can skip them wholesale. Two rules keep the pattern honest. The member name must be one your consumers provably ignore — check, do not assume — and the value must be a string or an object of strings, never a number or a structure a consumer might interpret. Annotations that masquerade as meaningful data are worse than no annotations at all, because they survive into systems that were never told to expect them.
External documentation
For files under version control, a README or a sibling CONFIG.md beside the JSON file carries annotations without touching the data at all. This scales better than inline notes: you can document entire sections, link to issues and decisions, and record why a value changed over time. The cost is discoverability — the explanation is no longer adjacent to the value. If that matters, keep a pointer in the data: a short "_comment" that says "see CONFIG.md, section 'retry policy'" costs one line and saves the next reader a search.
Schema descriptions
If the file's shape is enforced by a JSON Schema, the schema itself is the natural home for documentation. Every property definition accepts a description and a title, and most schema-aware editors surface those strings as hover text while the file is being written. The documentation then lives where the constraint lives, applies to every file that validates against the schema, and never appears in the transmitted data at all.
{
"type": "object",
"properties": {
"timeoutMs": {
"type": "integer",
"description": "Request timeout in milliseconds. Values below 2500 caused rate-limit failures in staging, March 2026."
}
}
}
This option has a compounding advantage: schema descriptions are enforced documentation. When a property is renamed or removed, updating its description is part of the same change, so the notes cannot silently drift away from the file they describe the way a README can.
Preprocessors, with a caution
A preprocessor that strips comments and trailing commas before parsing is a legitimate engineering solution, and several mature libraries exist for it — this is, in effect, what the JSONC dialect used by some editors' settings files is. The important discipline is what such a tool actually is: a transformation. It produces a different document from the one on disk, and the parser's verdict applies to the transformed text, not the original.
That distinction matters most when the input is not yours. Running a lenient parser or a stripping pipeline on data you did not author — an API response, a third-party export, an uploaded file — changes what you validate. A payload that survived stripping is not a payload that would have survived strict parsing, and downstream systems that do parse strictly will reject what your pipeline quietly accepted. The rule is simple: transformations are for documents you wrote and control; strict validation is for documents the outside world sends you. If you need to diagnose third-party data that arrives with comments, treat it as malformed and use the line and column in the parse error to locate the exact fault rather than normalizing the problem out of sight.
A preprocessor is a transformation, not a validator. Strip comments only from documents you authored and control — never as a courtesy for untrusted input, where silent normalization hides real errors from the strict parsers waiting downstream.
Trailing commas: a different failure mode
Comments are usually introduced on purpose. Trailing commas usually are not. They arrive through three mechanisms, and recognizing them is most of the defense.
Hand edits are the first. Deleting the last member of an object is the classic case: the author removes "debug": true from the end of a configuration, leaves the comma after the new last member, and the file no longer parses. The edit looked clean because editors do not flag a dangling comma the way they flag a missing one, and the human eye does not either.
Programmatic generation is the second. Code that builds JSON by concatenating strings — a shell script, a template, a log formatter — appends a comma after every member and forgets to remove the final one. Concatenation-based generation is the root problem; generation logic that emits a comma only before a subsequent member is immune to it.
Diffs and merges are the third. A merge that takes the last line of one branch (a member with its trailing comma, written while a different member came after it) and the closing brace of another produces a file that neither branch ever contained. Version control reports a clean merge; only a parser reports the truth. This is one more reason the order and layout of arrays and members deserve deliberate conventions rather than whatever the last merge happened to produce — arrays whose element order is meaningful are especially exposed, because a trailing comma there often signals that elements were shuffled as well.
$ git merge feature/remove-debug
# config.json now ends:
{
"retries": 3,
"timeoutMs": 2500,
}
# merge reported success; the parse fails at line 4
It bears repeating that unlike JavaScript, and unlike JSON5 and a few other derivatives, standard JSON has never allowed the trailing comma — not in objects, not in arrays, not in any version of the specification. There is no legacy-compatibility argument to be made; the grammar has been unambiguous on this point since the first RFC in 2005.
A recovery workflow for polluted documents
When a document fails to parse because of comments or trailing commas, resist the urge to fix things one at a time until it happens to go through. A disciplined sequence both repairs the file and proves that the repair changed nothing that matters.
1. Validate strictly first. Parse the untouched file with a strict parser and read the first error. It names a line and column, which is usually the offending / or comma — but not always. Some parsers report the position where they gave up, which can sit several tokens after the actual fault. Read the message as a starting point, not a verdict.
2. Remove or relocate comments. Delete them, or move their content into a "_comment" member, an external README, or a schema description — whichever fits the file's role. Relocating is usually better than deleting: comments that mattered once tend to matter again, and the person who wrote them was trying to help.
3. Strip trailing commas. Remove every comma that precedes a closing } or ]. After the first pass, search the file for ,} and ,] to catch any that were introduced while editing.
4. Revalidate. Parse again. If new errors appear, they are independent faults — unquoted strings, single quotes, missing colons — and each deserves its own deliberate fix. Repeat until the document parses.
5. Confirm the value is unchanged. This is the step people skip, and it is the one that catches real damage. If a copy of the document exists from before the edit or merge, compare the parsed values, not the bytes. Removed comments and normalized formatting must not change any value; if the parsed result differs from what was expected, a deletion went too far, and it is far cheaper to notice now than after the file has been consumed by the system it feeds.
$ python3 -m json.tool config.json > /dev/null && echo OK
OK
$ diff <(jq -S . config.expected.json) <(jq -S . config.json)
# empty output: values identical, formatting aside
The workflow is deliberately boring. Its value is that it ends with evidence — a parse success and a value comparison — rather than with the mere absence of an error message.
Test it yourself
The fastest way to internalize these rules is to break them on purpose. Open the JSON Inspector, paste the commented document from the top of this guide, and read the error. Then remove the comments, add a trailing comma after the last member, and watch the reported position move. Fix both, revalidate, and finally paste in a third-party response containing a comment to see how a strict parser treats input you did not author. Two minutes of deliberate failure will teach you more than reading the specification ever will.
| Construct | Verdict in strict JSON | Alternative |
|---|---|---|
// line comment | Parse error | "_comment" member, README, schema description |
/* block comment */ | Parse error | Same options as line comments |
"comment": "text" member | Valid — it is ordinary data | Fine, if consumers provably ignore the name |
Trailing comma before } | Parse error | Omit the comma after the final member |
Trailing comma before ] | Parse error | Omit the comma after the final element |
| Comments in YAML, TOML, JSON5, HJSON | Valid there, never in JSON | Keep the file in that format, or strip comments in a pipeline you own |
| Stripping preprocessor on third-party data | Hides real errors downstream | Strict validation, no silent normalization |
Comments and trailing commas were excluded from JSON on purpose, by a designer who preferred a grammar every implementation agrees on over a grammar every human finds comfortable. The exclusion has held through three RFCs and is not going to be relaxed. The workable response is not to fight the parser but to give annotations a legitimate home — a designated member, external docs, or a schema description — and to treat any lenient parsing of data you did not write as the bug it usually is. The grammar's refusal to bend is precisely why it has needed no revisions in twenty years.
Reproduce every example locally with the JSON Inspector or the matching tool on the workbench. Nothing you paste leaves your browser.