json: adding comments

2022-01-10

 | 

~1 min read

 | 

144 words

The JSON specification does not actually support comments, but sometimes it would be really handy to be able to comment your JSON.

There are a few options here:

  1. If feasible, change your language server from JSON to JSONC
  2. Use a custom key to indicate a comment

Changing your language server is definitely the simplest solution, but it doesn’t always work.

For example, while some applications allow comments in their JSON (VSCode’s settings files work just fine for example), others don’t (npm).

There are parsers for JSONC, a superset of JSON, to JSON, e.g., Go JSONC Parser and Node JSONC Parser here.

If you can’t use JSONC’s language server, what can you do?

Well, how about custom keys?

{
    "key":"value",
    "comment//":"here's a comment!"
    "version":1.0.0,
    "number":42,
    "comment2//":"here's another comment"
}

Comment keys do not need to be unique, however they should be.



Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!