I just stumbled across this hack for adding comments in JSON and there is no greater testament to human ingenuity and endurance in the face of adversity.
@deech omg! Why is it an array though? To conform to a contextual schema, I suppose?
@deech
In which sense is this a comment?
@monnier @deech in the sense that the author hopes whatever program is interpreting this JSON file will ignore this key. (Not a safe assumption, but JSON doesn't leave a lot of options...)

@deech @emilybache

I usually do this:
{
ā€œAuthorā€:ā€ā€¦ā€,
ā€œAuthorCommentā€:ā€This is a comment for Authorā€,
}

@deech JSON5 extends the JSON syntax, allowing for single and multiline comments (https://json5.org/)
JSON5 – JSON for Humans

JSON for Humans

JSON5

@deech There several variants of JSON that allow comments.

YAML is a superset of JSON but is also more complex. Then there’s JSON5, HuJSON and JSONC, JWCC and more but none are widely popular.

https://nigeltao.github.io/blog/2021/json-with-commas-comments.html

JSON With Commas and Comments

Nigel Tao

@markstos @deech yaml is ALMOST a superset of JSON. Basically everyone claims it’s a superset because it’s so close to being one, but it’s not.

https://john-millikin.com/json-is-not-a-yaml-subset

JSON is not a YAML subset

John Millikin

@masukomi @deech The article you linked to says the latest/current version of YAML is a proper superset of JSON.

If you want to use YAML as ā€œJSON with comments and trailing commasā€, you can.

@deech I don't quite get it, is `//` anything special, of just something with as a silly name as any random set of Latin letters that would just make the consumer ignore it? And why a list? A scalar is not enough? And isn't the trailing comma after the string a Syntax Error?

@mdione @deech It's just a key that the consumer is likely to ignore. And somebody pointed out elsewhere in the thread that the value of using an array is to make the "comment" appear on multiple lines when the JSON is formatted in a human-readable way.

I think the trailing comma would be a syntax error, yeah, but IMO the image makes its point well enough even if you ignore that.

@deech It’s so easy to just use JSON5. I don’t know why everybody doesn’t just use JSON5. (For content that’s expected to be human written and readable.)
@evolvable @deech approximately none of the tools I’ve used that take JSON inputs will accept JSON5 or JSONC or anything but pure JSON, so I can’t ā€œjust use JSON5ā€ I’d have to convince an awful lot of people to change software they publish and have to wait for the updated versions to be released and compatible with everything I need them to interact with. I can’t speak for anyone else, but that’s why I don’t; ā€˜cause it’s not actually so easy
@ShadSterling @evolvable @deech I'm pretty sure you're speaking for a *lot* of other people, even if very few of them are in a position to identify themselves here
@diazona @ShadSterling
Sorry, I actually meant people writing software that ingests JSON should just ingest JSON5 instead, as that is generally a trivial change.
My bad, I didn’t explain what I meant very well.
@deech
@deech @shanselman fun fact: while this isn’t ā€œofficialā€ comment syntax, NPM accepts it in package.json. So, if you have it in your ā€œdependenciesā€ fields, those ā€œcommentsā€ will be ignored. At least, this was true 5 years ago.
I used to to do the double "declare" to do comments but relies on the parser picking the right value which is not portable. This scheme I think is better

@deech Hmm, but this means you can have only one comment in an object, because an object can contain only one element with a given key, no?

Ahh šŸ’” but actually you could also have "//1": "I am the second comment" in the same object!

@deech until the app complains about an unexpected element in the json object..
@deech ez, make a macro that types this in in a fraction of a second
@Jessica @deech jsonc lets you do comments without this horrible syntax too
@deech We accept "-// arg" and "-//=arg" etc as command line options that are just comments in all our test programs so that if we have some complicated command line of options set up for debugging etc we can "comment out" some of those option to temporarily remove them non-destructively.
Also helps to comment label commands in bash history etc (altho TBH I tend to use '_="some comment label" some-command args ...' for that instead)
@deech Reminds me that I wonder how the different ASTs used by formatters/linters/… store comments.
@lanodan @deech you can use a "red-green syntax tree", a flexible trees structure whose leaves are tokens (incl. comments and whitespace) and whose nodes group tokens and other nodes together. They are loosely typed and can losslessly represent invalid syntax, whitespace and comments.
They're usually used to implement error-tolerant language servers but can work for formatters too.
@deech Just added a comment to a project's JSON file and now I feel empowered to do anything.
@deech I have also seen pairs of keys called "thing" and "//thing", where the former is data and the latter is human readable documentation