Home » Php » php – Add json file comments

php – Add json file comments

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:

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 🙂

Answer:

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.