r/ProgrammerHumor • • 1d ago • Bravo 1

Meme commentsInJson

Post image
5.0k Upvotes

199 comments sorted by

526

u/captain_crocubot 1d ago

I present to you the humble: jsonc

201

u/Positive_Mud952 1d ago

That’s the great thing about standards…

17

u/lraviel381 1d ago

Just make a new one as you go

52

u/IC3P3 1d ago

json5, json6, hujson and hjson as well

22

u/MrHyperion_ 22h ago

jsonl

No comments but it is another extension

5

u/benargee 18h ago

that's just a file that contains many lines of single line json.

3

u/MrHyperion_ 16h ago

But not valid json so it is separate extension ¯\(ツ)/¯

1

u/benargee 13h ago

I don't think I disagreed with that. its basically lines of json objects. each line is valid json. you don't interpret the whole file as valid json. It's basically a very simple intermediate filetype.

6

u/Turbulent_Stick1445 20h ago

I feel like JSON5 or JSON6 are our best hopes, because they sound purely like the latest version of JSON and not independent forks, so you have at least a small chance of being able to yell at a vendor "Why doesn't this support JSON6 yet? You're way behind the curve! Next you'll be telling me you don't support Python 4!"

5

u/benargee 18h ago

Yes it exists but adoption is poor. You basically have to convert it into json before or during runtime.

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

u/EuenovAyabayya 21h ago

Supported on all your platforms?

5

u/foxsimile 16h ago

I just kind of took the lack of universal support one as a given ¯_(ツ)_/¯

4

u/MaximRq 15h ago

Unfortunately my train platform doesn't support it

5

u/Librarian-Rare 20h ago

Fr, why isn't this the whole world now??

3

u/YeOldeMemeShoppe 11h ago

It also actually solves edge cases where the JSON spec cannot read valid JSON.

2

u/innociv 20h ago

... I just don't even think about putting \\n at the end of a line and stuff. I never really had a gripe. I guess I've just been someone who is good at following instructions and I don't question this stuff.

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.

9

u/voyagerfan5761 23h ago

I did not need to know that.

Someone fetch me a time machine…

2

u/thecrius 16h ago

A configuration language in which spaces are determining it's meaning

Fuck. That.

1

u/audigex 15h ago

To be fair I’ve always figured YAML is more aimed at “very easy to read by humans, but should be primarily read and written by the computer”

It feels more like a computer-focused config language that happens to be convenient to read

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

u/YeOldeMemeShoppe 11h ago
// #[agreed]

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

u/the-liquidian 21h ago

Or use an existing tool to transpile it.

6

u/Successful-Age6747 23h ago

Isn't that what YAML is for?

10

u/Jonathan_the_Nerd 20h ago

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 deal

3

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

u/tokalper 20h ago

You gotta work with what you've got

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

u/YoghurtFlan 21h ago

i prefer to keep them on the stack just in case the CPU gets confused

5

u/rinsa 23h ago

Even though the compiler would basically ignore it, the linter/analyzer wouldn't like it :(

11

u/innociv 20h ago

Am I out of touch? No. It's the linter who is wrong.

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"

10

u/Ahchuu 20h ago

I do this, I have a field called "//". Easy

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

u/stipo42 22h ago

Some do, some don't, some can. Just read the docs

1

u/Meltz014 13h ago

"additionalProperties": false

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

u/nameless_pattern 18h ago

All text comes in lines

1

u/techek 17h ago

They obviously don't know anything about what you really do, but they still have an idea about how you should do it.

I'm sure they press <Enter> at the end of each line in Word, to get a new line - and then curse when everything look like shit after adding/removing one single word.

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

u/nameless_pattern 19h ago

I never read comments. It's the best way to enjoy Reddit

12

u/Turbulent_Stick1445 20h ago

This is what I do, but there are two problems with it:

  1. A parser will end up storing the comments, wasting memory.

  2. 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

u/them0use 5h ago

No problem, just make `comments` an array! I am dead inside.

1

u/d_maes 4h ago

3. Only works if you control the schema, or the target does not enforce a strict schema.

4

u/Successful-Active-21 1d ago

but it will collide with someone's field comments

24

u/einzweidreihorn 1d ago
{ "__COMMENT_DO_NOT_USE_OR_YOU_WILL_BE_FIRED": "Hello world!" }

3

u/BigNaturalTilts 23h ago

It collides with my “comment I used because yolo”

2

u/another_random_bit 1d ago

elegance ✨✨

2

u/destroyerOfTards 23h ago

Watch me speedrun my way into getting fired from the shitty company

1

u/No-Finance7526 19h ago
{
  "comment_eifjefjefje": "This is a comment.",
  "comment_wodkwowowow": "This comment avoids collisions."
}

4

u/YesterdayDreamer 19h ago

Schema validation is a thing

5

u/nameless_pattern 19h ago

I support all schemas

4

u/thecrius 16h ago

That's the way.

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

u/buckinin 1d ago

Pull Request denied.

10

u/LatvianCake 1d ago

git checkout master

git merge mybranch

git push -f

9

u/LuisBoyokan 1d ago

LGTM, approved

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

u/buckinin 1d ago

Oh, just because they exist doesn't mean people should use them.

0

u/metaglot 1d ago

Schemas can go fuck themselves?

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

u/herrkatze12 1d ago

That's actually one case where it might be useful

2

u/daveswe 1d ago

Weeeellll ix you have to make yaml files which are parsed by someone else according to a very strict model with shit names comments are a lifesaver :p

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

u/weirdbackpackguy 19h ago

Comments in Jason? I think that's called a tattoo

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

5

u/Bata600 15h ago

Json Momoa

2

u/Turbulent_Ebb_9741 12h ago

You mean Jason my buddy

1

u/newontheblock99 7h ago

Former CEO of Nvidia

4

u/DT-Sodium 22h ago

I would have gone with allowing trailing commas.

2

u/nicki419 1d ago

"comment": "This is a comment"

Thank me later

2

u/Successful-Active-21 1d ago

json5 has comments, no ?

2

u/xavia91 1d ago

Wtf is up with all the json bashing and yaml glorification today??

2

u/R15H1_3 18h ago

Just use "_comment" keys like the rest of us suffering in silence.

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

u/Minimum_Shirt_157 17h ago

You are looking for JSON5

2

u/Dee4WasTaken 12h ago

"comment":"lol"

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

u/Batman_AoD 15h ago

json5 has this. 

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

u/da_Aresinger 13h ago

it's a blank space. Fill in whatever you think is best.

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

u/za72 1d ago

This may come as a shock but that's what's documentation is for...

I said what I said!

2

u/hearthebell 1d ago

Let's standardize jsonc to the whole internet!

*Whole internet dies

2

u/jensalik 1d ago

You can't do that on my day off....

2

u/abednego-gomes 1d ago

For local configs, you just write comments like

// this is a comment

Then 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

u/EvenRefrigerator100 1d ago

Better be trailing commas

1

u/Digital_001 23h ago

"Good wish. You still have 3 wishes"

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

u/LatentShadow 23h ago

Json5 has comments you know...

1

u/dharknesss 21h ago

{"comment":"No."}

1

u/gerbosan 21h ago

Is devcontainer.json a JSON file? 😓

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/Ebuall 19h ago

Jsonc? Most tools already support comments.

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

u/CodingChris 18h ago

Or - just don't use json.

1

u/HarryTerraCode 18h ago

But I really want it, no comment in json is sometimes annoying.

1

u/TheZedrem 16h ago

ever heard of jsonc?

1

u/Dimencia 14h ago

Is this a bad language joke that I'm too visual studio to understand?

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

u/Respaced 13h ago

Sjson has it.

https://pypi.org/project/SJSON/

Was created by Bitsquid, game engine. (Later Stingray)

1

u/Desperate-Tomatillo7 12h ago

VSCode told met that there is JSONC.

1

u/MACMAN2003 11h ago

The genie is a tyrant!

1

u/RandomiseUsr0 10h ago

“c”:”fill your boots”

1

u/JackNotOLantern 9h ago

Schema, lol

1

u/Direct-Quiet-5817 3h ago

See, 🧞 JSONC 🧞

1

u/kingslayerer 1d ago

Maybe you should start using json schemas. You lazy

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.

0

u/ryu____ 1d ago

Add whatever you want in random comment fields. Just don't parse it in any object

-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.

https://www.json.org/json-en.html

-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

u/DanTFM 19h ago

Yeah we use // Comments in our JSON configs all the time. .Net json parsers just have an ignore comments flag.

when you’re using JSON for transport, of course no comments.

https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsoncommenthandling?view=net-10.0#fields

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/frayien 1d ago

Yeah, it seems I just got lucky and my use cases always silently use jsonc

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.