JSON is one of the widely used formats to exchange data between front-end and back-end of software application, but there can be possibility, you might want to add comments in JSON file, although it is not possible to add comments in JSON file or JSON data, but there are few alternative ways which you can try.

JSON does not support comments and everything we write inside a JSON is considered as data only. JSON contains keys and values as per standard rules, and these can be parsed by different parsers using programming libraries.

So if you try to add comments in JSON file like below, it would be invalid

{
  "key": "value" // Invalid JSON, we cannot add comment here
}

So, let take a look at few alternatives, one by one.

Adding Data as Comments

One of the easiest way to add comments in JSON is to add it as key-value pair.

We will have to ignore these key-value pair when parsing JSON data, but sometimes it can be useful.

{
  "key": "value",
  "_comment":"This is just a comment"
}

Another example with a little bit more data in JSON

{
	"id": "1",
	"type": "icecream",
	"name": "Vanilla Cone",
	"_comment":"This is vanilla cone comment"
},
{
	"id": "2",
	"type": "icecream",
	"name": "Choclate Cone",
	"_comment":"This is Choclate cone comment"
}

Using HJson

HJson is a syntax extension to JSON.

It's NOT something to replace JSON or to incorporate it into the JSON spec itself.

It's intended to be used like a user interface for humans, to read and edit before passing the JSON data to the machine.

Using HJson, you can easily add comments in JSON, here is an example when using HJson

{
    // use #, // or /**/ comments,
    // omit quotes for keys
    key: 1
    // omit quotes for strings
    contains: everything on this line
    // omit commas at the end of a line
    cool: {
        foo: 1
        bar: 2
    }
    // allow trailing commas
    list: [
        1,
        2,
    ]
    // and use multiline strings
    realist:
        '''
        My half empty glass,
        I will fill your empty half.
        Now you are half full.
        '''
}

Using JSON5

JSON5 is again a library that is an extension to the popular JSON file format that aims to be easier to write and maintain by hand (e.g. for config files).

Example of using JSON5

{
  // comments
  unquoted: 'and you can quote me on that',
  singleQuotes: 'I can use "double quotes" here',
  lineBreaks: "Look, Mom! \
No \\n's!",
  hexadecimal: 0xdecaf,
  leadingDecimalPoint: .8675309, andTrailing: 8675309.,
  positiveSign: +1,
  trailingComma: 'in objects', andIn: ['arrays',],
  "backwardsCompatible": "with JSON",
}

When parsing JSON5 data, you can use its parser also

JSON5.parse(text[, reviver])

Hope this helps.

You may also like to read:

Nested and Complex JSON examples

Read JSON file with Javascript

How to read JSON data in C# (Example using Console app & ASP.NET MVC)?