Comments are piece of text string about the line of block of code and these are ignored by processor of that file.

Does JSON supports Comments?.

As per Standard JSON rules, There is no official support for Comments in JSON content.

JSON contains key and values as per standard rules, and these can be parsed by different parser using programming libraries.

SO Even if json allow comments, You need to have support for parser to read and understand the comments.

If you write like this json file with content, it is invalid json file

"key": "value" // comments are not allowed and invalid

You can achieve multiple ways to have a comments in JSOn object.

One way is using adding key value pairss with comments and description JSOn always contains data of key and values, so add comments key in json object with value is comment string value.

"key": "value",
"comments":"One way to write comments here"

You need to have parser or processor to read and ignore the comments key in a code

Second way, use JSOn minify library by removing the spaces and c language comments from the json document before parsing the json string

For example

let jsonString="{
"key": "value",
 // Single line comments
/* Multi line comments


JSON.minify removes the white spaces and comments from json string

Here is an output

"key": "value",

Third way is using javascript variables.

JSON is about javascript object, so declare json data in a javascript variable and add the comments in json format as seen below.

const jsondata = { 
"name": "john", 
//comments: javascript comments in json string 

This is not pure json comments, but javascript json object with comments which are ignored by javascript compiler.

Why JSON file not allow comments?

JSON is an open standard data exchange between Client and server which contains data of key and values

Server produces the JSOn data and consumers or clients consumes the data json from Server, SO there is no explaination of what is data about in the form of comments.

Another reason, if you add comments, It makes the human and library parser difficult to read the json data.

Comments are usually added to source code to describe about lines of code. JSON is purely about data format to send, Hence It is not feasible to add comments to JSOn


As JSON does not support comments, You can still add comments in multiple ways