Can comments be made in JSON?

We use JSON for many applications today, but can we make comments within a JSON?

This is the most common communication format between APIs, is the basis for creating objects, and is also the communication format with some NOSQL databases, such as MongoDB.

Often, when we’re developing, we need to make some kind of comment in our files to document what that means or else remind us of something that still needs to be done.

However, if you simply try to make a comment in your text editor in a JSON file, you will see that an error will appear.

comment on json

So can we make a comment?

Is it possible to comment on JSON?

For simplicity, no! That’s not possible.

JSON is a format that should consist only of data. That is, any other type of information other than in the format: name -> value.

But then how to document a JSON or put our famous TODO: for future modifications?

Well, there are a few ways you can work with that. The first is simpler:

Just use the areas of the application that generate or call your JSON and add your comments just above that call.

Another interesting way, but that will slightly increase the size of your JSON objects and documents is to add an entry called “_comentario", or "_comment” or with some name that makes sense to you. Example:

{
    "_comentario": "Here's your comment on this JSON,"
    "property": "value",
    "property 3": [
        {
            "prop1": "value 1",
            "prop2": "value 2"
        },
        {
            "prop3": "value 1",
            "prop4": "value 2"
        }
    ]
}

By doing this, you can highlight what you need to comment on in your JSON while not needing to use this information in your application.

When you make a request for this JSON, you can simply ignore this property.

I want to comment on my JSON anyway. What can I do?

If you do not like the above alternatives and wanted to give a little way to comment on your JSON, there are also other people who are in the same boat as you.

Even a developer created a solution called JSON.minify. And I also found another guy who developed a similar solution that performs a little better. Check out the links above!

The great thing about these solutions is that you can create your JSON with comments normally. Your text editor will probably acknowledge the error, but you can ignore it since it has something in your code that will help you.

To implement this solution, use the code from the repositories above, and when calling your JSON, do so as follows:

JSON.parse(JSON.minify(my_str));

What this function does is go through a process of removing comments and empty spaces before being called to your application.

Personally, it’s not something I would use because I prefer to make my code simpler, but I understand that there are people who might be more interested in this option.

Conclusion

The purpose of JSON is to store data and provide a way to exchange data between applications. Therefore, the use of comments within it is not possible.

What did you think of that explanation? Is there anything else you think is important to mention? Leave in the comments.

Enjoy!

Gostou do artigo?

Share on facebook
Facebook
Share on linkedin
LinkedIn
Share on pinterest
Pinterest
Share on whatsapp
WhatsApp
André Lug

André Lug

Leave a Reply

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