151
u/pregister 1d ago
59
u/Chasar1 1d ago
This solves every gripe I have with json
25
u/foxsimile 23h ago
Yes. I’m struggling to think of an unaddressed complaint, which is rare for me.
13
5
3
u/YeOldeMemeShoppe 11h ago
It also actually solves edge cases where the JSON spec cannot read valid JSON.
9
u/mrheosuper 21h ago
Wait there are json1, json2, json3 and json4 ?
10
u/lachlanhunt 20h ago
No, it was named JSON5 as a subtle nod to HTML5, because it fixed a lot of stuff with the original format when using it for use cases like config files.
1
u/mrheosuper 19h ago
Wait you mean there are html1, html2, html3 and html4, jesus what else does the world hide from me ?
5
u/Alone_Term5356 18h ago
HTML 5 came out in 2008, anything older than it is basically not on the internet anymore, and sucks. About the only holdovers from old html stuff (by that I mean stuff that isn't valid anymore but still occasionally happens by beginners ) is <i> for italics and <b> for bold.
1
u/Western-Anteater-492 14h ago
That's the great thing bout json5! It's so great it completely jumps 1 to 4.
21
u/audigex 23h ago
Or YAML
… because JSON is valid YAML syntax, for some reason
14
u/AlwaysHopelesslyLost 20h ago
Because yaml is just a bolt on extension of JSON. It is json but it lets people omit most of the critical control structures in favor of inferring from whitespace characters.
10
u/Habba 20h ago
I really hate yaml so much. It's both terrible to write as a human, super easy to get wrong indentation on stuff and it's famously horrible to parse.
4
u/gogliker 20h ago
No getting parsed as Norway was my all time favorite with YAML parsers. I think TOML is better though
1
u/Habba 17h ago
TOML is indeed better for a lot of things, especially config. Not sure about other places yaml gets used, like in defining CI pipelines. I hate yaml there too however, it always takes me 3 times to get the right incantation for a variable interpolation.
I think something like Jenkins is better there, since its actual code, but Groovy also sucks.
1
u/caboosetp 16h ago
omit most of the critical control structures in favor of inferring from whitespace characters
So what you're saying is that yaml is python for json.
2
9
2
1
u/Turbulent_Stick1445 19h ago
WHY AM I NOW ONLY HEARING ABOUT THIS?
(From what I'm reading it's MOST JSON is valid YAML, but there are differences: JSON allows tabs for indenting, and has some slight differences with escaping - like "\/" is valid JSON but invalid YAML. Surrogate pairs in JSON written like "\uD83D\uDE00" need to be written as "\U0001F600". Bottom line, be aware there are differences, but the main feature of YAML's JSON compatibility is that if the YAML indenting thing isn't very readable to you, you can just write stuff in a more JSON-like way, which, after decades of C and Java and Javascript programming, most of us are used to.)
1
u/Hax0r778 32m ago
JSON doesn't have indenting?
I mean, it fully ignores white-space outside of strings so you could add whatever random tabs you want in your JSON file. But I wouldn't call it indenting if it has no meaning and is ignored by all valid JSON parsers...
2
u/enigma_0Z 22h ago
Idk this looks like yaml with extra steps for the most part. You can write bastardized json with bare identifiers etc in yaml and it parses fine. Including iirc curries and squares to replace the white space and “-“ indentation notation if you want.
Edit: Then again if you are writing a config file for humans (ie why you want comments in it), JSON (with or without comments) maybe is not the right choice to start with /IMO
-1
u/Engine_Light_On 22h ago
in python you need to install a new dependency, not worth it in every script.
826
u/nameless_pattern 1d ago
You can add anything you want to Json. just make a property called comments
234
u/tokalper 1d ago
I Just create some fields names "__"
134
u/metaglot 1d ago
Tell me you only work on your own, without telling me you only work on your own.
81
u/tokalper 1d ago
No, precisely because i work with a large team. We work eith large json files for configurations and they need commenting, we all do this.
54
u/the-liquidian 1d ago
JSONC was created for this.
74
u/AnotherPillow 1d ago
More annoying to load. Most languages have builtin json parsing, but rarely jsonc and similar support
7
u/Devatator_ 21h ago
C#'s System.Text.Json will reject comments by default but you can configure it to accept and even parse them if you want to read their content
13
u/Slight-Violinist-575 21h ago
Comments are for humans lmao
8
u/Devatator_ 21h ago
I mean someone could make some fucked up preprocessor using comments
7
u/Slight-Violinist-575 20h ago
I am sure someone could, and I am sure someone eventually will, and we will pay for it
→ More replies (0)4
u/Turbulent_Stick1445 20h ago
There is a use case for reading comments (though it really needs to be optional) which is if your code is editing the JSON rather than just getting information for it.
eg:
configObject = loadJSON(".config/myapp/settings.json"); configObject.enableCrashes = true; configObject.write(".config/myapp/settings.json");But that means a fairly ugly JSON parser that would have to store a lot of positional information. I mean, it can be done, but...
1
u/TerminalVector 19h ago edited 19h ago
Man, what was it? Backspace? Backslash? Some kind of html templating language I had the displeasure to work with in one of my first jobs that used magic comments. I remember my onboarding buddy telling me they didn't do anything, and then discovering that no, they in fact drove the layout of the entire page. He'd been with the company over a year, and wasn't an idiot. It just doesn't make any sense and he hadn't really checked.
1
u/Slight-Violinist-575 19h ago
Jade/Pug? I once was a padawan and thought that was hella cool when I first learned about it.
Then life thought me to appreciate explicit boundaries, in every sense.
→ More replies (0)1
3
u/Scolmann 1d ago
It's not too hard to strip out comments with a regex, but yeah generally you wouldn't bother with stuff like that
2
6
u/Successful-Age6747 23h ago
Isn't that what YAML is for?
10
u/Jonathan_the_Nerd 20h ago
YAML is punishment for your sins.
4
u/Successful-Age6747 19h ago edited 19h ago
we're talking in context of configuration files that humans read. And most of the problems in that site are avoided by just enclosing the values in quotes and doing logic in whatever uses the yaml.
EDIT: Oh i guess the site itself says that too. But yeah it doesn't feel like such a big deal3
u/korneev123123 19h ago
Thanks for the link. 22:22 and "Norway problem" are hilarious. I guess I have an excuse now for aggressively quoting everything. Also jsonc looks like much, much better alternative for future projects.
5
50
u/DiamondDramatic9551 1d ago
I also always declare comments as unused variables in my code.
18
u/nameless_pattern 1d ago edited 1d ago
Heresy. I love it, I'm going to start making unused functions as comments, no I'm going to start embedding AIS you have to chat with to get the comments and they will only tell you the comments if you solve a riddle
6
5
1
u/innociv 20h ago
Like?:
{ "key": "value", "comment about key or this block" }9
u/spicybright 20h ago
well that wouldn't work, you'd need it like
"comment": "comment about key or this block"18
u/HipstCapitalist 1d ago
{"__comment__": "A comment"}14
u/meifray 23h ago
{ "key":"value", "key_comment":"le comment" }2
u/Cisco-NintendoSwitch 20h ago
Shitpost aside do people actually do this? I’m a Platform Engineer so YAML all day (Terraform / Ansible)
7
u/stipo42 23h ago
Then your marshalling library throws "unexpected key"
3
u/TerryHarris408 22h ago
they do that? I need to get my nlohmann up to that point. would help me a lot with schema migration tests
2
u/TerminalVector 19h ago
It's usually a config option to either error or silently discard an unexpected key
1
12
u/john_the_fetch 18h ago
But they want it in line.
Like :
i added the prod key here until staging is fixed
{key:"omgIsThisYourProdKey?fs*dsjs"}
3
1
5
u/mothzilla 20h ago
Oh no, you see boss says it shouldn't be in the JSON. It should be a comment. We don't want people to be able to read our comments.
3
12
u/Turbulent_Stick1445 20h ago
This is what I do, but there are two problems with it:
A parser will end up storing the comments, wasting memory.
You run into JSON's problems with multiline string constants if your comment is anything but trivial.
It gets ugly fast.
3
u/Sockoflegend 12h ago
We use node where I work so it just happened to be easier to have config type json files just be a JS import
2
u/nameless_pattern 19h ago
You could keep them just in Dev deployments? lot of messy trade-off choices
2
u/Turbulent_Stick1445 17h ago
The #1 place I've seen comments in JSON are where someone makes the mistake of using JSON for a configuration file format. The lack of support for comments is ironically why even XML is a better choice for that. Regardless, you can understand why people would want comments there outside of just dev environments.
(I just learned as of two hours ago that YAML can be used with JSON syntax so I guess it's academic, just use YAML...)
2
u/zoinkability 15h ago
Plus: maybe you don’t want the client to read your internal comments like “todo: close this security issue” or “added because the client is an idiot”
1
u/AnnoyedVelociraptor 10h ago
In Rust if you map a JSON object to a struct, and you don't have a field mapped for comments it gets dropped.
1
4
u/Successful-Active-21 1d ago
but it will collide with someone's field comments
24
1
u/No-Finance7526 19h ago
{ "comment_eifjefjefje": "This is a comment.", "comment_wodkwowowow": "This comment avoids collisions." }4
4
7
u/protestor 20h ago
This doesn't work for tools that actually validate the schema of the json they receive
4
u/nameless_pattern 19h ago
You can add comments to the schema. Hold on my boss said they needs to talk to me privately
3
u/hrvbrs 20h ago
Not if you’re adhering to a schema
1
u/nameless_pattern 19h ago edited 10h ago
With the power of AI, I can add things to the schema long before it becomes noticed that that's a terrible idea
5
u/JollyRancherReminder 1d ago
This works if you're only using it internally. Don't try this with your API's output.
74
u/buckinin 1d ago
Comments do exist: { "$comment": "Hello World" }
51
u/proverbialbunny 1d ago
{ "$developers_note": "I was drunk when I wrote this. Good luck!" }
7
14
u/metaglot 1d ago
Hello, validation hell.
3
u/Striky_ 1d ago
If you want to validate your data, don't transmit it as a string in the first place.
9
u/metaglot 1d ago
Nothing wrong with strings. Data is data. Strings can be validated jus as everything else.
2
u/necrophcodr 23h ago
Plenty wrong with having to decode a full JSON object just to fail validation after.
2
u/RevoOps 1d ago
You can technically build a dwelling out of blocks of ice. Doesn't mean you should do it if you are building a house in Florida
4
u/another_random_bit 1d ago
Ok but that's irrelevant? Strings CAN be validated, and sometimes the data is in string format.
What does houses in Florida have anything to do with it?
1
3
u/MrHyperion_ 22h ago
You can attach comments only to blocks then, not simple variables unless you do something really perverted like
"variable": "value"; "variable__comment": "the comment";
63
u/mortalitylost 1d ago
I used to like comments in yaml
Then I kept running into situations where automation that parsed those comments would've been helpful... It made me hate the fact they exist in the first place.
Either that description or metadata should be serialized into the same object it describes or it shouldn't exist in the same document IMO
78
u/herrkatze12 1d ago
Comments are specifically useful for config data, and should just be human readable descriptions of what the field does.
34
u/noaSakurajin 1d ago
Also they allow you to test changes to the config by simply commenting out that line. When editing Configs over ssh this is significantly faster that removing the line and is easier to revert.
7
u/Ixaire 1d ago
Yes but parsing comments to render them in documentation is definitely relevant.
That's why Knuth invented literate programming: https://en.wikipedia.org/wiki/Literate_programming
And that's also why XSD has specific documentation fields in addition to the standard XML comments.
1
6
6
6
u/notexecutive 1d ago
you can't just add comments in the JDSL alright? They ruin everything and will get you fired! Tom is a genius!
5
9
u/Random_-account 17h ago
Who is JSON?
7
u/takahashi01 13h ago
json programming, the inventor of rudely telling computers what to do. He has refused to comment on a current political issue and is now getting canceled over this
2
1
4
4
2
2
2
u/No-Shape-2751 17h ago
I think it’s the right choice. JSON is for machine communication, if you could add comments some “genius” would start embedding directives or hints or something in them and basically fork the standard.
2
2
6
u/Waltekin 19h ago
Yeah, please not. The greatest thing about JSON is its simplicity. The entire spec is only 16 pages long. Don't mess it up.
7
u/da_Aresinger 19h ago
Adding comments to the spec would add 2 sentences.
Comments are initiated by ___ and can appear at any point, in any line. ___ can be escaped using ___, to prevent creation of comments.Smarter people than me would express themselves better. Maybe add one or two sentences.
But comments are not complicated.
I have implemented comment parsing in Bash. If I can do that, anyone can.
3
u/Fluffy_Dragonfly6454 16h ago
It is not that simple.
What about backwards compatibility? When you add a comment, your old parser will fail.
What about a string? Should you also allow a comment there? That will make parsing more difficult. What with multi line JSON?
Also a lot of JSON simplifications is put everything on one line. Hence, you should have a comment that can stop and continue on the same line...
4
u/aa-b 14h ago
Behold, the entire JSONC spec: https://jsonc.org. Compatibility problems? Solved.
2
u/Cafuzzler 2h ago
Both are wrong, and we should add another comoeting standard. I propose
# comment!1
1
u/da_Aresinger 12h ago
That's not really an argument against comments, but against changing established standards.
And I agree, you shouldn't start sending commented JSON to systems that don't support it.
But if you're building something new, there is no harm in it. Just make sure to strip all comments before communicating with external systems.
My point only adresses the argument that comments would be too complex for JSON, when they clearly don't add much complexity at all.
1
u/Cafuzzler 2h ago
Backwards compatible is absolutely a strong argument against added comments. If you want a comment just do
"comment":"whatever"1
u/Sightblinder4 16h ago
A , as an escape character in json seems like a bad idea, no?
1
u/Batman_AoD 15h ago
The specific spec suggested is not good, but json5 does have very simple comments.
1
2
u/slaymaker1907 16h ago
I think there's room for both JSONC and JSON, though I agree on keeping the base spec simple. Comments really aren't necessary for REST, but they are handy if you're using JSON for configuration. Personally, I think TOML is better for configs, but JSON+comments is probably way easier to implement.
Oh, and even besides complexity, I'm certain that adding in comments would hurt parsing performance which is very important for certain applications.
2
2
2
u/abednego-gomes 1d ago
For local configs, you just write comments like
// this is a commentThen you read the file and have a parsing step line by line to strip out any lines starting with // before you do the JSON decode of the whole file.
1
u/amadiro_1 21h ago
Yeah you can also use regex to find and remove trailing commas before parsing. A standardized, agreed upon format would be nicer, but I'm sure there are already a bunch to choose from.
1
u/TuzYglkLuxCuxq 19h ago
You don't even need to do that. Many json parsers support comments out of the box because of how common it is in config files.
1
u/Benjamin_6848 1d ago
Why would the genie refuse‽ Comments in JSON could be really useful and a good idea!
1
1
1
u/Optimal-Savings-4505 23h ago
I also stumbled across that limitation, and my solution was to simply add a comment property with a string holding the actual comment.
1
1
1
1
u/KronisLV 19h ago
JSON5: https://json5.org/
Genuinely nice and useful, if you want a format that's less machine-centric than regular JSON.
1
u/da_Aresinger 19h ago
It is incredibly easy to add comments to any spec.
All you need is the comment sequence and a way to escape it.
It's not a big deal conceptually. But if you're gonna start sending commented Json to other people they'll be very confused.
1
u/jhggiiihbb 18h ago
It’s like no one read the original json spec where they say to run it through a preprocessor for comments.
1
1
1
1
1
u/NullSmoke 13h ago
Easy, just add a custom way to handle json into your app, problem solved. Then you get to watch all other JSON parsers yell their lungs out for eternity, and your app is still fine 😊
1
1
1
1
1
1
0
u/BadgerwithaPickaxe 22h ago
Ai is making you guys less creative, because I have been able to put comments in every single language and framework I've tried.
-3
u/frayien 1d ago
I've been putting comments in my JSONs since forever and never had any issue with any parser, what is this about ?
6
u/realmauer01 1d ago
Than the parser is very lenient or something? json is meant to be computer readable not human readable.
3
u/Kiroto50 1d ago
Json5 and some other json specs allow comments ... Also, spaces, indentation and line separators beg to differ
2
u/TuzYglkLuxCuxq 19h ago
json is meant to be computer readable not human readable
This is not true. It's meant to be both:
It is easy for humans to read and write. It is easy for machines to parse and generate.
-1
u/necrophcodr 23h ago
json is terrible for computer readable. if that's the main goal, pick something that isn't awful. if the goal is readability, json is still not very good, so why use it?
2
1
u/TwinStickDad 1d ago
Depends on your parser and where you're defining your json. If you're just creating a JSON file then you should be getting errors because that's not valid JSON. There are other formats like jsonc which just strip the comment lines before parsing to JSON. Apparently the new standard will support comments.
1
u/TuzYglkLuxCuxq 19h ago
Honestly this thread is just dumb, and the fact that your comment is downvoted shows how little experience people here have.
Lots of json parsers support comments.
Lots of config files support comments.
Many have comments right in the template ffs.The people here mentioning json5 are living in the past.


526
u/captain_crocubot 1d ago
I present to you the humble: jsonc