Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can comments be used in JSON?

Tags:

json

comments

Can I use comments inside a JSON file? If so, how?

like image 370
Michael Gundlach Avatar asked Oct 28 '08 20:10

Michael Gundlach


People also ask

Why are comments not allowed in JSON?

Reasons to use JSONC and not to allow comments in the regular JSON files are: It will separate your file from real JSON files. It is not going to bite you in the back when you add comment to a file where validation has to be applied, but you forget to remove comments because there is no error message.

How do you comment out a block of code in JSON?

Open the JSON file in your text editor and add comments the same way you would in Python (using # and not docstrings) or the same way you would in JavaScript (using // and not multi-line comments using /** */ ).


2 Answers

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"             }          }       }    } } 
like image 100
Eli Avatar answered Nov 25 '22 05:11

Eli


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)
like image 39
stakx - no longer contributing Avatar answered Nov 25 '22 06:11

stakx - no longer contributing