Is it possible to comment on a JSON file?

10

Can I comment on a JSON file? If yes, how?

    
asked by anonymous 05.05.2015 / 20:08

4 answers

18

No. Unable to comment on JSON.

    
05.05.2015 / 20:11
11

The ECMA-404 standard, which specifies the JSON standard, does not provide any provisioning for comments.

In cases where comments are strictly necessary, post-parsers can be implemented to manipulate payload in order to eliminate any extra content and keep the output compatible with any services consumers. But there is no established standard.

Sources:

05.05.2015 / 20:22
7

No. It has been made to not contain comments, it is just a data structure formatted as an object.

NOTE If you need some kind of comment, you can insert another field inside your object called comment or something like ...

    
05.05.2015 / 20:12
0

The specification does not allow, but if you have already sold your soul to the devil and someone is pointing a gun to your head, you can apply a gambiarra.

For a parser that does not get JSON through streaming , if the same key is declared more than once, the value that will ultimately count whichever comes last. Then, to comment a key, create another key of the same name above it. So:

{
    "pessoa": "A pessoa a quem este arquivo se refere",
    "pessoa": {
        "nome": "O nome da pessoa",
        "nome": "John Doe"
    }
}

Just emphasizing what I have said before, but in other words: if you do this above you solve a problem now, but you go straight to hell soon after.

I got this idea from SOen's most controversial post to this day:

Can comments be used in JSON?

    
31.08.2017 / 21:34