Proposal Details

Add support for the JSON with Comments specification, in a new package, called encoding/jsonc.

Given Go's native and first-class support for encoding/json, there is common usage of JSON for configuration files, given it's "too easy not to" use it, and allows not reaching for an additional library to parse i.e. YAML, TOML, JSON5 etc.

This would make it provide first-class support for comments in JSON, allowing for improved experience for folks using JSON for configuration file formats.

In scope

  • Single-line comments
  • Multi-line comments

Out of scope

  • Trailing commas:

JSONC doesn’t allow trailing commas; however, we encourage parsers to be lenient and handle trailing commas gracefully where possible to reduce the risk of human edits introducing parsing errors.

Usage

Similar to encoding/json

Other considerations

  • [ ] How will performance be benchmarked?

Common libraries

Libraries in the ecosystem that provide JSONC support:

  • https://github.com/marcozac/go-jsonc
  • https://github.com/tidwall/jsonc
  • https://github.com/muhammadmuzzammil1998/jsonc

See also

  • https://jsonc.org/
  • https://codeberg.org/JSONC-Spec/JSONC-Spec

Comment From: ianlancetaylor

I don't think it makes sense to duplicate encoding/json into encoding/jsonc. But it seems feasible to provide an io.Reader implementation that strips JSON comments. Ideally this would be done in the context of encoding/json/v2 and encoding/json/jsontext (see #71497).

Comment From: jamietanna

Sounds reasonable - I was pondering about whether there's something we can do with encoding/json/v2, especially if we wanted to allow folks to set trailing commas as an opt-in

Comment From: seankhliao

See https://github.com/golang/go/discussions/63397#discussioncomment-7202224

I believe the conclusion for the moment is https://go.dev/doc/faq#x_in_std

Comment From: jamietanna

the conclusion

I assume this is some off-issue discussion that's happened?

It's fine if it is, but wanted to confirm in case I'd missed anything!

Aside: https://go.dev/doc/faq#x_in_std is the link to follow here (note the second underscore)

Comment From: seankhliao

71497

Support JSON5 or JWCC: This is out of scope for v2 and can be a future proposal if those related JSON formats become sufficiently popular.