Trailing Commas and JSONC
Why Trailing Commas Aren’t Part of the JSONC Specification?
Trailing commas are not part of the JSONC Specification because the reference implementation, jsonc-parser, does not allow them unless explicitly configured. The allowTrailingComma
option is set to false
by default, so any trailing comma will result in a parsing error.
The reason why this specification chose the default behavior of the parser as the reference for the standard is to ensure that JSONC remains compatible with the broader JSON ecosystem, which does not allow trailing commas. This decision helps maintain consistency and predictability across different parsers and implementations. Namely, the TSConfig and ESLint config files, which are widely used in the JavaScript ecosystem, do not allow trailing commas in their JSONC files.
The exclusion of trailing commas also facilitates the creation of tools and libraries that can parse JSONC without needing to handle additional syntax variations. This helps ensure that JSONC remains a lightweight and straightforward extension of JSON, primarily focused on adding comments without introducing significant complexity.
Can a Parser That Chooses to Support Trailing Commas Still Be Considered a JSONC Parser?
Yes, however this is not part of the JSONC.org specification and such support would be considered an extension or variation of the standard JSONC format. This means that while a parser may allow trailing commas, it may not be compatible with all JSONC parsers or tools that strictly adhere to the JSONC specification without trailing commas.
Trailing Commas in VS Code
The “JSON with Comments” mode in VS Code used to allow trailing commas without any warnings by default, but this was eventually changed to discourage their use and promote better compatibility with other JSONC parsers (source).
At the time of writing this document, the “JSON with Comments” mode still accepts trailing commas, but it discourages their usage by displaying a warning (source) unless the file is one of the VS Code official configuration files. The exclusion of those configuration files comes from the JSON schema used. The schema for these files explicitly allow trailing commas, which is why they are accepted without warnings in that specific context.