Can I comment a JSON file? If so, how?
You can't. At least that's my experience from quick glance to json.org
Json has its syntax visualized on that page. No note from comments.
Typically, JSON is generated or parsed by some other language where comments are allowed. I've never really heard of a JSON "file", it's usually more of a transient, over-the-wire type thing - and it's kind of nightmarish for humans to read anyway.
I don't believe you can have an actual comment. The JSON should all be data, and if you include a comment, then it will be data too.
You could do that with a data element called "_comment" (or something) that would be ignored by apps that use the json data.
You would probably be better having the comment in the processes that generate/receive 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"
}
}
}
}
}
The idea behind JSON is to provide simple data exchange between applications. These are typically web based and the language is javascript.
It doesn't really allow for comments as such, however, passing a comment as one of the name/value pairs in the data would certainly work, although that data would obviously need to be ignored or handled specifically by the parsing code.
All that said, it's not the intention that the JSON file should contain comments in the traditional sense. It should just be the data.
Have a look at the JSON website for more detail.
Not allowing comments makes JSON useless for configuration files. Stupid mistake.
If your text file, which is a JSON string, is going to be read by some program, how difficult would it be to strip out either c or c++ style comments before using it? Answer: It would be a one liner. If you do that then JSON files could be used as configuration files.
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. Hopefully that's helpful to those who disagree with why JSON.minify() could be useful.
You should write a JSON schema instead. JSON schema is currently a proposed internet draft specification. Besides documentation, the schema can also be used for validating your json data.
Example:
{"description":"A person",
"type":"object",
"properties":
{"name": {"type":"string"},
"age" : {"type":"integer",
"maximum":125}}
}
You can provide documentation by using the description schema attribute.