JSON Comment Tutorial

JSON or JavaScript Object Notation is a data format, or object notation used to store information in a simple human-readable and easy to consume format. JSON is not a programming or scripting language but used by Programming and scripting languages like JavaScript, PHP, Python, C#, Java, etc. The JSON does not provide a native way to comment on data. The JSON is data only format but provides an extensible structure where different and new data easily added.

JSON Does Not Support Comment

First of all JSON doesn’t support comments and there is no official and standard way to create comment in a JSON object or JSON data. This is very normal as the JSON is a data or object format which is created to exchange data and data do not need comment. The comment is generally used in programming and scripting languages to explain code.

Create Comment Field In JSON

Even JSON does not provide comments by default we can use the JSON key in order to create our own comment structure. The general assumption for JSON objects is underscored before the key definition means the key is metadata. So we can create a new key with an underscore in its name and behave it metadata for comment. The following example JSON stores the name and surname values with the _comment key which is used for commenting metadata.

{
   "_comment": "This is a comment",
   "person": {
      "name": "İsmail",
      "surname": "Baydan"
      }
   }
}

Create Comment with 2 Underscores

As there is no standard way to create a comment in JSON there are a few different implementations where 2 underscores can be used to define a comment. The first underscore is prefixed with comment and the second postfixed after the comment.

{
   "_comment": "This is a comment",
   "person": {
      "name": "İsmail",
      "surname": "Baydan"
      }
   }
}

Leave a Comment