Tuesday, May 29, 2012

Can I comment a JSON file?

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

Source: Tips4all


  1. 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 have a designated 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"

  2. 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.

  3. No, comments in JSON are not allowed. This answer is based on:

    RFC 4627:
    The application/json Media Type for JavaScript Object Notation (JSON)

  4. 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.

  5. 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.

  6. 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.


    {"description":"A person",

    {"name": {"type":"string"},
    "age" : {"type":"integer",


    You can provide documentation by using the description schema attribute.

  7. Consider using YAML. It's nearly a superset of JSON (virtually all valid JSON is valid YAML) and it allows comments.

  8. The Dojo javascript toolkit (at least as of version 1.4), allows you to include comments in your JSON. The comments can be of /* */ format. Dojo consumes the JSON via the dojo.xhrGet() call.

    Other javascript toolkits may work similarly. If anybody finds one, please edit this response and include it.

    This can be helpful when experimenting with alternate data structures (or even data lists) before choosing a final option.

  9. Diito - justy encountering this for config files. I don't want to use XML (verbose, graphically, ugly, hard to read), or "ini" format (no hierarchy no real standard etc) or java "Properties" format ( like .ini )

    JSON can do all they can do but way less verbose and more human readable - and parsers are easy and ubiquitous in many languanges. It's just a tree of data. But out of band comments are a necessity often to document "default" configurations and the like. Configs are never to be "full documents" but trees of saved data that can be human readable when needed.

    I guess one could use "#": "comment", for "valid" JSON :)

  10. 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.

  11. Comments are not an official standard. Although some parsers support c-style comments. One that I use is JsonCpp. In the examples there is this one:

    // Configuration options
    // Default encoding for text
    "encoding" : "UTF-8",

    // Plug-ins loaded at start-up
    "plug-ins" : [

    // Tab indent size
    "indent" : { "length" : 3, "use_space": true }

    jsonlint does not validate this. So comments are a parser specific extension and not standard.

  12. Just ran into this very issue. Comments are not allowed, however there is no explicit statement of this fact in the docs (I guess the productions at json.org are explicit). I put comments in a json configuration file, jsonsimple parser just returns null, no exceptions. BTW, the json lint at www.jslint.com says that json with comments is valid.