How to Comment in JSON Files

How to Comment in JSON Files

Are you having trouble to add comments on your JSON file ? The answer of the question is pretty obvious YES. This is because JSON doesn't support commenting . However there is a simple solution to this, which we are going to take a look at in this blog. So let's get started !!!

You must be familiar with the way how we declare variables and functions private in classes in java. We simply put " _ " in-front of the variable name or function name and everything with the syntax like this becomes private. Albeit privatizing them, we can directly access and make changes to the file. What i mean to say is , privatizing variables and methods is not legalized in js instead js people have made this available just for our convenience itself. In the similar fashion we have not been provided with the facility to add comments in JSON file.But there is a solution for this.Let's see what it is !!

Add Data as Comments:

A way to skirt around the comments issue is to add data to your JSON file that function as comments.Let’s go through an example, starting with this information in our JSON file:

    {
     "name":"Anna",
    "age":"19"
     }

Now let’s add another key-value pair to serve as our comment, which you can see in the first line in the code below:

    {
    "_comment": "this is a comment",
     "name":"Anna",
    "age":"19"
     }

The underscores help to differentiate the comment from the rest of the data in our file.

KEEP IN MIND :

The comments we added to our JSON file are included in the JSON object. In other words, the comments are treated as data. So when we access the the file, along with all the data the comment is also extracted along with the key and its value.

While there are several other tricks to add comment in JSON file, but the one mentioned in the article is the simplest of them!!

I hope you have got something new to read and learn about !!!

Thanks For Reading :)