Comment Inside Json : How to Add Comments in JSON File?

Dec 19, 2022

4 mins read

Published in
Comment inside JSON

JSON (JavaScript Object Notation) is a data format that makes it easy to transport data between client and server, like pulling information from a database. Comments in JSON files are the same as other types of comments - they describe what's happening in the code.

What is the use of Comments?

Comments are used to explain the code or why we are using certain parts of the code. Comments are also an excellent place to record changes since they do not break the program.

If we have comments, it might be easier to understand the code and its behavior. This means that comments are an essential part of programming language to maintain the original intent and make it easier for someone who inherits our code (or even us) to understand it better.

Comment in JSON

How to use the comment in JSON? Does JSON Specification support JSON?

JSON does not support Comments like // or /* */

JSON specification does not support comments like //, /* */, <– -> in JSON files. Here is the reason for this. Douglas Crockford is the founder of the JSON data specification.

What Douglas Crockford said regarding comments in JSON Files?

He removed comments from JSON because he saw individuals were using them to hold parsing directives, which would have ruined interoperability.

Because of this, Douglas Crockford has removed the comments from JSON specification due to misuse, and it can increase the size of the JSON data payload. So comments are not permitted in JSON.

Find more information about JSON Specification at json.org.

Can I add comments in JSON? Of course…

Here is the JSON data with the comment field inside the JSON file. Based on the comment name, it can be fetched to process or used as information about JSON data.

Comments inside JSON data

1
2
3
4
{
    "_commentTest":"Testing the Comments",
    "comment1":"user can assign the comment name as per the requirement"
}

JSON Example with data and Comments

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
"InsuranceCompanies": {
  "comment1":"This json will have all the top InsuranceCompanies's data",  
 "Top Insurance Companies": [
  {
   "No": "1",
   "Name": "Berkshire Hathaway",
   "Market Capitalization": "$661 B",
   "comment2": "do you know who is the ceo of this company?"
  }
   ],
   "comment3":"This data is updated on 19th Dec 2022"
 }
}

Should we use JSON comments as Data Fields?

When we add a comment as a data field in JSON, it increases the size of the payload of the JSON, and if the JSON data contains more comments, it will lead to significant performance issues and cost more to store/transfer data.

These kinds of comments can be used in these scenarios:

  • Educational Purpose
  • Configuration files
  • Testing purposes

Know More about JSON Comments:

How to add comments in package.json?

In a package.json file, comments are added using a double forward slash (//) at the beginning of the line. The JavaScript runtime does not interpret these comments and is only used for developer documentation and notes. For example:

1
2
3
4
5
6
7
8
9
{
  // This is a comment
  "name": "my-project",
  "version": "1.0.0",
  // More comments can be added here
  "dependencies": {
    "lodash": "^4.17.20"
  }
}

It’s worth noting that comments in JSON files are not officially supported, and some tools and libraries may not handle them properly. However, most JSON parsers will ignore comments and parse the file without issues.

If the above method does not work, add a comment as a value. For example:

Adding comments as Value in package.json

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
  "name": "my-project",
  "version": "1.0.0",
  "dependencies": {
    "lodash": "^4.17.20"
  },
  "comments": {
    "dependencies": "Comments are necessary to understand complex stuffs",
    "description": "Add more detailed information.",
    "license": "add license information"
  }
}

Related Links:

Sharing is caring!