Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add json file comments [duplicate]

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?

like image 227
Ash Blue Avatar asked Jun 25 '12 18:06

Ash Blue


People also ask

Can I add comments in JSON file?

Can I add a comment in JSON? No, JSON is a data-only format. Comments in the form //, #, or /* */, which are used in popular programming languages, are not allowed in JSON. You can add comments to JSON as custom JSON elements that will hold your comments, but these elements will still be data.

How do I comment out multiple lines in JSON?

Adding Multiple-Line Comments in JSON Since JSON and strictly-linted JavaScript don't allow duplicate keys of values, you can add a unique letter or number to make it validate: { "//a": "This is the first comment.", "//b": "This is the second second." }

Why are comments not allowed in JSON?

Reasons to use JSONC and not to allow comments in the regular JSON files are: It will separate your file from real JSON files. It is not going to bite you in the back when you add comment to a file where validation has to be applied, but you forget to remove comments because there is no error message.

Can JSON file have duplicate keys?

We can have duplicate keys in a JSON object, and it would still be valid. The validity of duplicate keys in JSON is an exception and not a rule, so this becomes a problem when it comes to actual implementations.


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

like image 166
Rich Bradshaw Avatar answered Oct 17 '22 16:10

Rich Bradshaw