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?

2
  • 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
    Commented Jun 25, 2012 at 18:33
  • Looked for this earlier, but it wasn't showing up in my searches.
    – Ash Blue
    Commented Jun 25, 2012 at 18:44

2 Answers 2

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 :)

2
  • 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. Commented Jun 25, 2012 at 18:32
  • 2
    Yes, this isn't something that I'd use – but it could be an option. Commented Jun 25, 2012 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.

9
  • 5
    Storing a comment in the data itself is really ugly IMO. @AshBlue: json.org Commented Jun 25, 2012 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
    Commented Jun 25, 2012 at 18:32
  • 4
    @Jashwant reputation doesn't mean anything
    – Esailija
    Commented Jun 25, 2012 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
    Commented Jun 25, 2012 at 18:33
  • 3
    Added source to answer. Thanks ThiefMaster.
    – tskuzzy
    Commented Jun 25, 2012 at 18:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.