JSON Comments Tutorial with Examples – POFTUT

JSON Comments Tutorial with Examples

One of the most asked JSON related questions is “Can we create or use comments in JSON data format?”. This question answer is a bit tricky where we can yes or no. We will answer whether we can create and use comments in JSON in this tutorial from all points of view.

We Can Not Create Comment In JSON Formally

Let’s start with bad news. JSON’s official definition or standard does not support comments. As a simple data format the standard committee though that this is unnecessary to create comments in data format.

JSON5 Comments

JSON5 is a nonofficial standard created by the developers and published in the GitHub. with JSON5 some extensions to the JSON standard are created. One of them is comments which are supported by the JSON5. According to JSON5 single and multi-line comments are allowed. Here is an example of JSON5 comments. We can see that // is used to create a single-line comment where /* and */ is used to create a multi-line comment.

Using  _comment Data Element For Comment

As JSON does not officially support comments but comments are needed by the developers heavily they create alternative ways to create comments. One of them is using the _comment data element inside the JSON data like below.

Using  _comment Data Element For Comment

Using  _comment Data Element For Comment

Using  comment Data Element For Comment

We can also use directly the comment as a data element which is very similar to the _comment.

Using  comment Data Element For Comment

Using  comment Data Element For Comment

Using /* */ and // C and C++ Like Comments

Even JSON5 supports single line // comment and /* */ multi-line comments normal JSON do not support it. We can use them as below.

Leave a Reply

Your email address will not be published. Required fields are marked *