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.

{
   //First name is used provide the user first name
   "firstName":"Ahmet",
   "lastName":"Baydan",
   "age":7,
   "birth":"2012-01-01",
   /* 
      Address block provides the complete address of the user
   */ 
   "address":{
      "streetAddress":"21 2nd Street",
      "city":"Ankara",
      "state":"Ankara",
      "postalCode":6543
   },
   "phoneNumbers":[
      {
         "type":"home",
         "number":"212 555-1234"
      },
      {
         "type":"fax",
         "number":"646 555-4567"
      }
   ]
}

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.

{
   "_comment":"First name is used provide the user first name",
   "firstName":"Ahmet",
   "lastName":"Baydan",
   "age":7,
   "birth":"2012-01-01",
   "_comment":"Address block provides the complete address of the user",
   "address":{
      "streetAddress":"21 2nd Street",
      "city":"Ankara",
      "state":"Ankara",
      "postalCode":6543
   },
   "phoneNumbers":[
      {
         "type":"home",
         "number":"212 555-1234"
      },
      {
         "type":"fax",
         "number":"646 555-4567"
      }
   ]
}
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.

{
   "comment":"First name is used provide the user first name",
   "firstName":"Ahmet",
   "lastName":"Baydan",
   "age":7,
   "birth":"2012-01-01",
   "comment":"Address block provides the complete address of the user",
   "address":{
      "streetAddress":"21 2nd Street",
      "city":"Ankara",
      "state":"Ankara",
      "postalCode":6543
   },
   "phoneNumbers":[
      {
         "type":"home",
         "number":"212 555-1234"
      },
      {
         "type":"fax",
         "number":"646 555-4567"
      }
   ]
}
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.

{
   // First name is used provide the user first name
   "firstName":"Ahmet",
   "lastName":"Baydan",
   "age":7,
   "birth":"2012-01-01",
   // Address block provides the complete address of the user
   "address":{
      "streetAddress":"21 2nd Street",
      "city":"Ankara",
      "state":"Ankara",
      "postalCode":6543
   },
   "phoneNumbers":[
      {
         "type":"home",
         "number":"212 555-1234"
      },
      {
         "type":"fax",
         "number":"646 555-4567"
      }
   ]
}

Leave a Comment