Write a comment to JSON

Sponsored Link

You're using JSON in some configuration file or something, and you want to write a comment in the file, or you want to comment out the previous value and leave it to temporarily change the setting
s (I'm sure I'm not the only one)

I was basically having trouble writing comments in the JSON file, but the other day I was taught a good way, so I will write a note and share it.

If you want to write a comment, you can keep the comment as data.

Simply keep comments as one of the data that has been taken over by JSON rules.

Like this.

{
    "_comment": "Keep comments as data",
    "id": "taberu_salad_jp",
    "name": "Eat Salad"
}

As data, I think that it is good to set the key name to _(underscore) beginning or to decide the rule so that it is easy to understand not to use it.

It is one hand to empty the key name.

Like this.

{
    "": "Keep comments as data",
    "id": "taberu_salad_jp",
    "name": "Eat Salad"
}

There are times when I want to write more than two comments…?

Yes, there are times like that. However, it only has to have this as JSON data as well.

Like this.

{
    "_comments": [
        "Comment on line 1",
        "Comment on line 2"
    ],
    "id": "taberu_salad_jp"
}

I want to comment on each of the keys

In short, if it is JavaScript, it is a story that wants to do such a thing.

It's got a Twitter ID in it.
var id = "taberu_salad_jp";
Twitter Name
var name = "eat salad";

Let's comment on JSON like this.

There is such a way.

{
    "id": "Twitter ID",
    "id": "taberu_salad_jp",
    "name": "Twitter name",
    "name": "Eat Salad"
}

In JSON, if the keys are duplicated, those who wrote before are ignored. (So overwritten when referenced)

Therefore, you can write as described above. However, it is a little less readable.

Summary

Here are some ways to comment on JSON. Personally, I'm using _comment pattern at the top.

Which one to use is a matter of preference, but in team development, it's better to unify if you use it.

Copied title and URL