JSON does not support comments in the official specification; any attempt to add comments using /* */ or // will break most parsers. Instead, use a field within the object to simulate comments, such as a key named "_comment" or "note". This maintains valid JSON while conveying explanatory text.

  1. Add a new key-value pair where the key is "_comment", "note", or another descriptive name, and the value is a string explaining the purpose.
  2. Place the comment field adjacent to the data it describes, ensuring it appears before or after the relevant section.
  3. Use only double quotes for keys and string values, as single quotes are invalid in JSON.
  4. Ensure no trailing commas appear after the comment field or any other field, as this invalidates JSON.
  5. Validate the JSON using a parser or online validator to confirm syntax correctness.

Why JSON Does Not Allow Standard Comments

The JSON specification, defined in RFC 8259, explicitly excludes support for comments. The original design goal was to create a minimal, interoperable data format based on JavaScript object literals, but without executable code. Comments were omitted to prevent abuse as metadata containers or to avoid ambiguity in parsing. As a result, /* block comments */ and // line comments—common in programming languages—cause parsing errors when included in JSON files. Most parsers, including those in browsers and Node.js, will throw a syntax error if they encounter these.

How to Simulate Comments Using Fields

The only safe method to include comments in JSON is to use a key-value pair where the key indicates it is a comment. Common choices include "_comment", "note", "description", or "explanation". For example: {"_comment": "This section defines user settings", "theme": "dark", "auto_save": true}. The leading underscore in "_comment" is a convention to signal that the field is non-functional. This approach works because JSON permits any valid string as a key, and parsers ignore fields they do not use. Ensure the comment value is a string enclosed in double quotes, as unquoted strings or single quotes are invalid.

Where to Place Comment Fields in JSON Structure

Place a comment field immediately before or after the data it describes. For object-level comments, position the comment field at the top of the object. For field-specific notes, place the comment directly before or after the target key. For example: { "_comment": "User preferences block", "theme": "light", "_comment_theme": "Light mode reduces eye strain in daytime", "notifications": true } This keeps the context clear. Avoid placing comments at the end of large objects, as the association may be lost. Nested objects can have their own comment fields. Do not place comment fields inside arrays unless the array contains objects; JSON arrays must contain only values, and inserting a comment object breaks data expectations.

Validating JSON After Adding Comment Fields

After inserting comment fields, validate the JSON using a parser. Use built-in tools like JSON.parse() in JavaScript, which throws an error if syntax is invalid. Online validators such as jsonlint.com or vscode extensions can highlight issues. Common errors include: unquoted keys, single quotes around strings, trailing commas, or invalid characters in the comment text. Control characters (U+0000 to U+001F) must be escaped if included. The total file must parse as a single JSON value—either an object or array. If validation fails, remove the comment field temporarily to isolate the issue. Once the base structure is valid, reinsert the comment and recheck.

Alternatives to JSON for Config Files With Comments

When comments are essential, consider formats that natively support them. YAML allows comments using # and is widely used for configuration. Example: # Database settings host: localhost port: 5432 TOML supports comments with # and is common in tooling configs:

Log level

debug = true JSON5 is a superset of JSON that allows // and /* */ comments, trailing commas, and unquoted keys. Example: // API timeout settings timeout: 5000, retries: 3 However, these formats require specific parsers. YAML parsers are vulnerable to injection if untrusted input is processed. JSON5 is not supported in browsers by default. If interoperability with standard JSON tools is required, stick to JSON with comment fields rather than switching formats.

Common Errors When Adding Comments to JSON

The most frequent error is using /* or // syntax, which breaks parsing. Another is placing a comment as a standalone value in an array, such as ["a", /* note */, "b"], which is invalid. Trailing commas after comment fields, like {"_comment": "end",}, cause failures in strict parsers. Using single quotes (‘comment’) instead of double quotes (“comment”) also invalidates the file. To fix, replace /* */ with a "_comment" field, remove standalone comment entries in arrays, delete trailing commas, and ensure all strings use UTF-8 encoding with proper escaping for quotes and newlines. Always validate the file after edits.

How to Tell If a JSON Comment Worked

A comment has worked if the JSON parses without error and the comment field appears in the data structure when loaded. In JavaScript, parsing with JSON.parse() and logging the result should show the comment field. Example: const config = JSON.parse('{"_comment": "test", "value": 42}'); console.log(config._comment); // prints "test" If the parser does not throw an error and the comment field is accessible, the comment is valid. If the field is missing in downstream processing, it may be filtered out by the application. In that case, adjust the application logic to preserve comment fields or use a different key name. Never rely on comments being preserved automatically—they are data, not metadata.

Using Comments in JSON Configuration Files

In configuration files, comment fields help document settings for future editors. For example: { "_comment_db": "Database connection settings. Change host only in production.", "db_host": "localhost", "db_port": 3306, "_comment_cache": "Redis cache enabled by default. Set to false to debug.", "use_cache": true } This improves maintainability. However, avoid including sensitive data like passwords in comments. Comments are part of the data and may be logged or exposed. If a setting is conditional, use a boolean flag instead of a comment to control behavior. Never use comments to disable configuration—remove the field or set it to null. Applications should document their handling of comment fields in their configuration guides.

Tools That Support Comments in JSON-Like Formats

Some editors and tools support JSON with comments by using lenient parsers. VS Code, for example, allows // and /* */ comments in .jsonc files (JSON with Comments) and provides syntax highlighting. The file must be saved with a .jsonc extension. Other tools like TypeScript and Deno support JSONC for configuration. However, these files are not valid JSON and will fail in standard JSON.parse(). To use them, ensure all consumers of the file support JSONC. Convert to strict JSON by removing comments before sending to APIs or web clients. A script can automate this by parsing JSONC and outputting clean JSON, stripping comment-only fields or using a preprocessor.

Security and Maintenance Risks of JSON Comments

Comment fields in JSON are treated as data, so they can be logged, serialized, or transmitted. Avoid including sensitive information such as passwords, API keys, or internal notes. If a JSON file is public, assume all fields are visible. Comment fields increase file size and may affect performance if excessively large. Over time, comments can become outdated if not maintained alongside code changes. To reduce risk, use comments only for non-sensitive explanations, review them during updates, and remove obsolete ones. Applications should not rely on comment fields for logic, as they may be stripped by minifiers or validators.