Practical guide

JSON with comments: working with JSONC files

Strict JSON does not include comments. If a file contains // or /* ... */, it is not ordinary JSON even if the rest of the document looks familiar.

JSONC is a practical extension used by configuration files and developer tools. JSON All-in-One supports JSONC workflows, but that is not the same as promising every JSON5 feature or every editor-specific relaxation.

Strict JSON versus JSONC

Use strict JSON when you are exchanging data with an API or another system that expects the JSON standard. Use JSONC when the file is a human-maintained configuration and the receiving tool explicitly supports comments.

JSONC comments are source annotations. They can help explain a setting, but they are not values in the data model. If you later hand the file to a strict JSON consumer, comments must be removed or the handoff must use a verified strict output.

config.jsoncjsonc
{
  // Local development endpoint.
  "endpoint": "https://api.example.test/v1//status",
  "retry": {
    "attempts": 3,
    /* Keep this low for browser tests. */
    "backoffMs": 250,
  }
}
A production-style payment configuration demonstrates JSONC comments and trailing commas, the URL damage caused by regex cleanup, comment search, and a verified strict JSON handoff.

Avoid regex cleanup

The URL in the fixture contains // inside a string. That is not a comment. A simple regular expression that removes everything after // can corrupt valid data.

A real parser has to know whether it is inside a string, a line comment, a block comment, an object, or an array. This is also why a trailing comma in a JSONC object can be acceptable to a JSONC parser while still invalid in strict JSON.

dangerous regex ideatext
Unsafe cleanup idea: remove //.* from every line.

Prepare strict JSON handoff carefully

When another system expects strict JSON, produce and inspect a cleaned result through a verified workflow. The expected strict form of the sample removes comments and trailing commas but keeps string values such as the endpoint URL unchanged.

Syntax normalization and schema validation are different tasks. If you need a starting schema, use Generate JSON Schema from sample JSON.

expected strict JSON shapejson
{
  "endpoint": "https://api.example.test/v1//status",
  "retry": {
    "attempts": 3,
    "backoffMs": 250
  }
}

JSONC is not JSON5

Some tools use JSON5 features such as additional number spellings or object key relaxations. Comments working in one file does not mean every JSON5 feature is accepted in another.

JSON All-in-One also has NDJSONC support for line-delimited relaxed records, but NDJSONC is separate from ordinary JSON Lines. For record streams, read How to open JSONL and NDJSON files.

Troubleshooting

  • If strict JSON tools reject the file, check for comments and trailing commas first.
  • If search misses a comment, confirm comments are included in the search scope.
  • If a // appears inside a string, keep it as data.
  • If a downstream API rejects cleaned output, inspect the exact bytes you saved rather than assuming comments were the only issue.