views:

8682

answers:

8

Can I comment a JSON file? If so, how?

+14  A: 

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.

Cheery
+3  A: 

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.

mmacaulay
It does not really matter (wrt this question) whether JSON document is saved as a file or not -- ability to add comments is useful for documents. Alas, there is no comment construct in JSON.
StaxMan
“ability to add comments is useful for documents” — right, but I don’t think JSON is intended for documents. It’s intended as a data format for machines to read, hence comments are pointless. XML is document-centric, and I think intended to be human-readable.
Paul D. Waite
JSON is much more human readable than xml. XML is more powerful but for 99% of usecases JSON will work just fine.
caspin
+37  A: 

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"
                }
            }
        }
    }
}
Eli
I don't think this is inelegant at all - it's exactly what XML does. The comment becomes part of the resulting document and it's only by convention that the contents are ignored (XML comments are accessable via the DOM, XPath etc. you can use them to store data if you want. Doing so would be strange but not without precedent - <!--[if IE 7]>)
Joe Gauterin
It might pay to have some kind of prefix on the actual comment in case there's ever a valid field named comment: `"__comment":"comment text goes here...",`
Rob Fonseca-Ensor
This is what I ended up using. I have written a code generator to generate classes from a formal definition of json objects, something like (but much simpler than) http://json-schema.org/. Each property definition has a "description" property that used to be a comment. It stinks that I can't break up long comments into multiple lines though.
Juan Mendes
why not use comments for prefixing? ie "// comment" :)
alex
@alex - not sure I understand. You mean inside the field, or just ahead of the main doc? In the second case, it wouldn't be valid json...
Eli
+1  A: 

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.

Neil Albrock
It is true that JSON format does not have comments. Personally I think that is a significant mistake -- ability to have comments as metadata (not data) is a very useful thing with xml.Earlier draft versions of JSON specification did include comments, but for some reason they were dropped. :-/
StaxMan
+16  A: 

Not allowing comments makes JSON useless for configuration files. Stupid mistake.

khs4473
aye to that. I don't want comments for the sake of documentation - I want a convenient means to slash out bits and enable them later on...
Roland Bouman
A: 

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.

John T. Vonachen
+9  A: 

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.

Kyle Simpson
Just what I was looking for! Google V8 used to ignore comments, now it doesn't...
Stuart
+1  A: 

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.

raffel