39

Possible Duplicate:
Can I comment a JSON file?

We are using a .json file on a project. We want to know if we can add comments to the file and avoid crashing the JSON parser.

We've tried to do so with the following comment types, but they all crash the JSON file when it's parsed:

# I crash
// I crash
/* I crash */

Is there an acceptable form of commenting for JSON files?

Community
  • 1
  • 1
Ash Blue
  • 5,344
  • 5
  • 30
  • 36
  • 2
    do search for SO questions before posting one. Just googling for 'json comment' returned me the above link as 1st search result – sv_in Jun 25 '12 at 18:33
  • Looked for this earlier, but it wasn't showing up in my searches. – Ash Blue Jun 25 '12 at 18:44

2 Answers2

20

JSON doesn't support comments – which is good when you think about it. However, someone has made JSON5 (https://github.com/aseemk/json5), which does, and may be of use to you.

It's worth pointing out that this is just someones JSON-like project, and is not an official spec, but then I guess JSON is just someones XML-like project that people liked :)

Sirko
  • 72,589
  • 19
  • 149
  • 183
Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
  • 4
    At the creator of it states: "JSON5 is not an official successor to JSON, and existing JSON parsers may not understand these new features.". So if you are going to use a standard JSON parser (which you'll probably do), better don't use it. – ThiefMaster Jun 25 '12 at 18:32
  • 2
    Yes, this isn't something that I'd use – but it could be an option. – Rich Bradshaw Jun 25 '12 at 18:51
11

The standard JSON format doesn't explicitly support file comments. RFC 4627 application/json

It's a lightweight format for storing and transferring data. If the comment is truly important, you can include it as another data field like comments: "my comment".

e.g.

{
    name: "Bob",
    age: 5,
    comments: "I don't like him"
}

However if it's being used in this format, it really is just another piece of data. So ultimately, what you have to realize is that just because certain fields are there doesn't mean you have to use it.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • Please provide a source to back this up. – Ash Blue Jun 25 '12 at 18:29
  • 5
    Storing a comment in the data itself is really ugly IMO. @AshBlue: http://json.org/ – ThiefMaster Jun 25 '12 at 18:29
  • 1
    @AshBlue, at SO , usually you dont ask someone with 11.9 reputation for a reference to backup his/her statement. – Jashwant Jun 25 '12 at 18:32
  • 4
    @Jashwant reputation doesn't mean anything – Esailija Jun 25 '12 at 18:33
  • 7
    @Jashwant: I have 30k rep, but I'm not always right. Plus sources are a good idea to include anyway. – gen_Eric Jun 25 '12 at 18:33
  • 1
    Agreed that rep. does not validate an answer, I need a source that directly says comments are not supported from a legit source. – Ash Blue Jun 25 '12 at 18:34
  • 3
    Added source to answer. Thanks ThiefMaster. – tskuzzy Jun 25 '12 at 18:35
  • 1
    Reference is always good and nobody is always right. I just didnt like the tone of his statement. "back this up" – Jashwant Jun 25 '12 at 18:49
  • correction: The standard JSON format _explicitly does not_ support comments... There's no room in the JSON spec for implicit comment support, and this is by design. If a parser does support comments, then it's not a JSON parser. – slang Aug 18 '16 at 04:09