JSON Comments: What Is Valid and How to Annotate JSON
Standard JSON has no comment syntax. Learn which formats allow // and /* */ comments, how to comment out lines safely, and how to annotate and validate strict JSON.
Standard JSON has no comment syntax. A //, /* */ or # anywhere outside a string value makes the file invalid for any parser that follows the specification. You still have three practical ways to annotate JSON. You can use a JSON-like format that accepts comments, if the program reading the file supports it. You can store notes as ordinary data. Or you can keep the notes outside the file. This guide covers what is valid, the comment syntax of the common variants, how to comment out a line without breaking the file, and how to check the result.
Are comments valid in JSON?
No. The JSON grammar is defined in RFC 8259 and ECMA-404. It allows only objects, arrays, strings, numbers, true, false, null and whitespace between those tokens, and it has no comment character. So for standard JSON, there is no comment syntax at all. Strict parsers reject comments. These include JavaScript's JSON.parse, Python's json module and jq. Tools that read their configuration with a strict parser reject them too, and npm's package.json is a common example.
// Example: this is NOT valid JSON
{
// server settings
"port": 8080
}
Error wording varies by engine and version. JavaScript engines typically report an unexpected token '/'. Python's json module typically reports "Expecting property name enclosed in double quotes" when a comment sits where a key should be. Both messages include a position, line or column that points at the comment.
Formats that look like JSON and allow comments
Several formats extend or overlap with JSON and do accept comments. They only help when the program reading the file uses a parser for that format. Renaming a file to .jsonc or .json5 does not change what the consuming application accepts, so check that tool's documentation first.
| Format | Comment syntax | Notes |
| JSON (RFC 8259 / ECMA-404) | None | Any comment is a syntax error |
| JSONC (JSON with Comments) | // line comments, /* block comments / | Used by VS Code for its settings files and accepted by TypeScript in tsconfig.json |
| JSON5 | // line comments, / block comments / | Also allows trailing commas, single-quoted strings and unquoted keys |
| Hjson | #, // and / */ | A human-oriented configuration format |
| YAML | # line comments | Most JSON documents are also valid YAML 1.2 |
Comment syntax in JSONC and JSON5
// Example: valid JSONC and JSON5, invalid JSON
{
// A line comment runs to the end of the line
"port": 8080,
/* A block comment
can span several lines */
"host": "localhost",
"commentPrefix": "//"
}
Block comments cannot be nested. A /* inside a block comment does not open a second comment, and the first */ closes it. Characters inside a string value are always data. That is why "commentPrefix": "//" above is a normal string and not a comment.
How to comment out a line
In a JSONC or JSON5 file, put // at the start of the line. Then check the commas. JSON separates items with commas, so disabling the last item leaves a trailing comma on the line before it. JSON5 accepts trailing commas. JSONC parsers vary: some accept them, while others warn or reject them. Removing the comma is the safe choice.
// Example: before
{
"port": 8080,
"debug": true
}
// Example: "debug" commented out, trailing comma left behind (risky)
{
"port": 8080,
// "debug": true
}
// Example: trailing comma removed (safe in JSONC and JSON5)
{
"port": 8080
// "debug": true
}
In strict JSON you cannot disable a line in place. Copy the file first so the original value can be restored, then delete the entry. Renaming the key, for example from "debug" to "_debug", only works if the consuming program ignores keys it does not recognize. A program that validates keys may reject the file or behave differently, so test before relying on it.
Annotating strict JSON without comments
- Add a documentation key such as "_comment". The note becomes part of the data, so every consumer will receive it. A schema that forbids additional properties will reject it.
- In JSON Schema documents, use the "$comment" keyword. The JSON Schema specification reserves it for notes to schema maintainers.
- Do not repeat the same key in one object, such as several "//" keys. RFC 8259 says object names should be unique, and parsers handle duplicates differently. Many keep only the last value, which silently drops the other notes.
- Keep longer explanations in a separate document, or in the source code that generates the JSON.
- If you control the program that reads the file, switch to a comment-friendly format such as JSONC, JSON5 or YAML.
// Example: a note stored as data (valid JSON)
{
"_comment": "Port must match the load balancer setting",
"port": 8080
}
Converting commented JSON to strict JSON
When a file with comments must go to a strict parser, remove the comments with a tool that understands the format. Do not use a plain find-and-replace or regular expression. The sequence // can appear inside string values, and a naive pattern deletes real content along with the comments. The example below is illustrative.
# Illustrative example of what goes wrong
# Input line:
"commentPrefix": "//",
# Naive removal of everything from // to end of line:
sed 's|//.*||' config.jsonc
# Resulting line (string cut off, file now broken):
"commentPrefix": "
A safer approach is to parse the file with a library for its format and write the data back out as strict JSON. For JSON5 in Node.js, the json5 package provides JSON5.parse. For JSONC, Microsoft's jsonc-parser package and the strip-json-comments package both handle comments while leaving string contents intact. The following is an example script, not tested output.
// Example: convert config.json5 to strict JSON in a new file
const fs = require('fs');
const JSON5 = require('json5');
const data = JSON5.parse(fs.readFileSync('config.json5', 'utf8'));
fs.writeFileSync('config.json', JSON.stringify(data, null, 2) + '\n');
Conversion makes some changes you should know about before you run it:
• The comments themselves are discarded.
• Formatting is rewritten.
• Duplicate keys collapse to one value.
• In JavaScript, integers larger than the language can represent exactly lose precision.
To protect your content, write to a new file as the example does and keep the commented original as the source you edit. If the data holds very large integers, such as IDs, confirm they survive unchanged.
Check the result
Run the output through a strict parser. Each command below prints nothing but the success message when the file is valid JSON. It prints an error with a position when the file is not.
python3 -m json.tool config.json > /dev/null && echo "valid JSON"
jq empty config.json && echo "valid JSON"
If an error is reported, go to the line and column it names. Look for these leftovers:
• a comment
• a trailing comma
• a single-quoted string
• an unquoted key
The last three are JSON5 features that strict JSON rejects. Fix that spot and validate again. Finally, spot-check a few values against the commented source to confirm the data did not change.
Quick answers
- JSON comment character: standard JSON has none. JSONC and JSON5 use // and /* /. YAML uses #, and Hjson accepts #, // and / */.
- JSON comment line: only possible in comment-tolerant formats, where // starts a comment that runs to the end of the line.
- Commenting out an entry: in JSONC or JSON5, prefix the line with // and remove any trailing comma left behind. In strict JSON, back up the file and delete the entry instead.
- Adding notes to a strict JSON file: store them as data, for example under a "_comment" key, or keep them outside the file.