Can comments be used in JSON? Can comments be used in JSON? json json

Can comments be used in JSON?


No.

The JSON is data only, and if you include a comment, then it will be data too.

You could have a designated data element called "_comment" (or something) that should be ignored by apps that use the JSON data.

You would probably be better having the comment in the processes that generates/receives the JSON, as they are supposed to know what the JSON data will be in advance, or at least the structure of it.

But if you decided to:

{   "_comment": "comment text goes here...",   "glossary": {      "title": "example glossary",      "GlossDiv": {         "title": "S",         "GlossList": {            "GlossEntry": {               "ID": "SGML",               "SortAs": "SGML",               "GlossTerm": "Standard Generalized Markup Language",               "Acronym": "SGML",               "Abbrev": "ISO 8879:1986",               "GlossDef": {                  "para": "A meta-markup language, used to create markup languages such as DocBook.",                  "GlossSeeAlso": ["GML", "XML"]               },               "GlossSee": "markup"            }         }      }   }}


No, comments of the form //… or /*…*/ are not allowed in JSON. This answer is based on:

  • https://www.json.org
  • RFC 4627:The application/json Media Type for JavaScript Object Notation (JSON)
  • RFC 8259 The JavaScript Object Notation (JSON) Data Interchange Format (supercedes RFCs 4627, 7158, 7159)


Include comments if you choose; strip them out with a minifier before parsing or transmitting.

I just released JSON.minify() which strips out comments and whitespace from a block of JSON and makes it valid JSON that can be parsed. So, you might use it like:

JSON.parse(JSON.minify(my_str));

When I released it, I got a huge backlash of people disagreeing with even the idea of it, so I decided that I'd write a comprehensive blog post on why comments make sense in JSON. It includes this notable comment from the creator of JSON:

Suppose you are using JSON to keep configuration files, which you would like to annotate. Go ahead and insert all the comments you like. Then pipe it through JSMin before handing it to your JSON parser. - Douglas Crockford, 2012

Hopefully that's helpful to those who disagree with why JSON.minify() could be useful.