r/ProgrammerHumor • • 1d ago • Bravo 1

Meme commentsInJson

Post image
5.3k Upvotes

200 comments sorted by

541

u/captain_crocubot 1d ago

I present to you the humble: jsonc

205

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

51

u/IC3P3 1d ago

json5, json6, hujson and hjson as well

22

u/MrHyperion_ 1d ago

jsonl

No comments but it is another extension

5

u/benargee 1d ago

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

4

u/MrHyperion_ 23h ago

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

0

u/benargee 21h 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 1d 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 1d ago

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

863

u/nameless_pattern 1d ago

You can add anything you want to Json. just make a property called comments

239

u/tokalper 1d ago

I Just create some fields names "__"

138

u/metaglot 1d ago

Tell me you only work on your own, without telling me you only work on your own.

85

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.

57

u/the-liquidian 1d ago

JSONC was created for this.

76

u/AnotherPillow 1d ago

More annoying to load. Most languages have builtin json parsing, but rarely jsonc and similar support

8

u/Devatator_ 1d 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

16

u/Slight-Violinist-575 1d ago

Comments are for humans lmao

9

u/Devatator_ 1d ago

I mean someone could make some fucked up preprocessor using comments

8

u/Slight-Violinist-575 1d ago

I am sure someone could, and I am sure someone eventually will, and we will pay for it

→ More replies

5

u/Turbulent_Stick1445 1d 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 1d ago edited 1d 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 1d 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

1

u/YeOldeMemeShoppe 19h 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 1d ago

Or use an existing tool to transpile it.

5

u/Successful-Age6747 1d ago

Isn't that what YAML is for?

12

u/Jonathan_the_Nerd 1d ago

3

u/korneev123123 1d 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.

3

u/Successful-Age6747 1d ago edited 1d 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

5

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

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

5

u/rinsa 1d ago

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

11

u/innociv 1d ago

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

1

u/innociv 1d ago

Like?:

{
        "key": "value",
        "comment about key or this block"
}

9

u/spicybright 1d ago

well that wouldn't work, you'd need it like

"comment": "comment about key or this block"

10

u/Ahchuu 1d ago

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

18

u/HipstCapitalist 1d ago
{"__comment__": "A comment"}

14

u/meifray 1d ago
{
    "key":"value",
    "key_comment":"le comment"
}

2

u/Cisco-NintendoSwitch 1d ago

Shitpost aside do people actually do this? I’m a Platform Engineer so YAML all day (Terraform / Ansible)

7

u/stipo42 1d ago

Then your marshalling library throws "unexpected key"

3

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

It's usually a config option to either error or silently discard an unexpected key

1

u/stipo42 1d ago

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

1

u/Meltz014 20h ago

"additionalProperties": false

12

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

All text comes in lines

1

u/techek 1d 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.

1

u/Vesuvius079 5h ago

Which is worse, seeing this comment in json or the act it describes?

6

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

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

13

u/Turbulent_Stick1445 1d 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 19h 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 1d ago

You could keep them just in Dev deployments?  lot of messy trade-off choices

2

u/Turbulent_Stick1445 1d 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 22h 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 17h 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 12h ago

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

1

u/Turbulent_Stick1445 2h ago

I have done exactly that :( Now I know YAML can be written like JSON though I'm probably going to just switch to that in future as YAML has almost as much support as JSON. I'd only been avoiding it because I thought the indenting-is-meaningful syntax was mandatory.

1

u/d_maes 11h 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 1d ago

It collides with my “comment I used because yolo”

2

u/another_random_bit 1d ago

elegance ✨✨

2

u/destroyerOfTards 1d ago

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

1

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

4

u/YesterdayDreamer 1d ago

Schema validation is a thing

5

u/nameless_pattern 1d ago

I support all schemas

4

u/thecrius 23h ago

That's the way.

7

u/protestor 1d ago

This doesn't work for tools that actually validate the schema of the json they receive

4

u/nameless_pattern 1d ago

You can add comments to the schema. Hold on my boss said they needs to talk to me privately 

3

u/hrvbrs 1d ago

Not if you’re adhering to a schema

1

u/nameless_pattern 1d ago edited 18h 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.

158

u/pregister 1d ago

60

u/Chasar1 1d ago

This solves every gripe I have with json

26

u/foxsimile 1d ago

Yes. I’m struggling to think of an unaddressed complaint, which is rare for me.

13

u/EuenovAyabayya 1d ago

Supported on all your platforms?

5

u/foxsimile 1d ago

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

4

u/MaximRq 22h ago

Unfortunately my train platform doesn't support it

5

u/Librarian-Rare 1d ago

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

3

u/YeOldeMemeShoppe 19h ago

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

2

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

Wait there are json1, json2, json3 and json4 ?

10

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

Wait you mean there are html1, html2, html3 and html4, jesus what else does the world hide from me ?

5

u/Alone_Term5356 1d 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 21h ago

That's the great thing bout json5! It's so great it completely jumps 1 to 4.

21

u/audigex 1d ago

Or YAML

… because JSON is valid YAML syntax, for some reason

15

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

No getting parsed as Norway was my all time favorite with YAML parsers. I think TOML is better though

1

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

I did not need to know that.

Someone fetch me a time machine…

1

u/Turbulent_Stick1445 1d 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 7h 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...

1

u/Turbulent_Stick1445 2h ago

JSON has optional indenting where the tab is just generic white space you can use without it impacting anything. You do not need to indent something in JSON, and it's only meaningful to human readers if you do.

However, tabs ARE meaningful in YAML, under certain circumstances they affect how the file is parsed.

1

u/thecrius 23h ago

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

Fuck. That.

1

u/audigex 22h 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

2

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

in python you need to install a new dependency, not worth it in every script.

76

u/buckinin 1d ago

Comments do exist: { "$comment": "Hello World" }

52

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 1d 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_ 1d 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

79

u/herrkatze12 1d ago

Comments are specifically useful for config data, and should just be human readable descriptions of what the field does.

35

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!

10

u/Random_-account 1d ago

Who is JSON?

8

u/takahashi01 21h 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

6

u/Bata600 23h ago

Json Momoa

2

u/Turbulent_Ebb_9741 20h ago

You mean Jason my buddy

1

u/newontheblock99 14h ago

Former CEO of Nvidia

5

u/weirdbackpackguy 1d ago

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

4

u/DT-Sodium 1d ago

I would have gone with allowing trailing commas.

7

u/Waltekin 1d 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.

9

u/da_Aresinger 1d 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 23h 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 21h ago

Behold, the entire JSONC spec: https://jsonc.org. Compatibility problems? Solved.

2

u/Cafuzzler 10h ago

Both are wrong, and we should add another comoeting standard. I propose # comment!

1

u/Batman_AoD 22h ago

json5 has this. 

1

u/da_Aresinger 20h 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 10h ago

Backwards compatible is absolutely a strong argument against added comments. If you want a comment just do "comment":"whatever"

1

u/Sightblinder4 1d ago

A , as an escape character in json seems like a bad idea, no?

2

u/da_Aresinger 20h ago

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

1

u/Batman_AoD 22h ago

The specific spec suggested is not good, but json5 does have very simple comments. 

2

u/slaymaker1907 23h 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/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 1d ago

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

2

u/No-Shape-2751 1d 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 1d ago

You are looking for JSON5

2

u/Dee4WasTaken 20h ago

"comment":"lol"

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

"Good wish. You still have 3 wishes"

1

u/Optimal-Savings-4505 1d 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 1d ago

Json5 has comments you know...

1

u/dharknesss 1d ago

{"comment":"No."}

1

u/gerbosan 1d ago

Is devcontainer.json a JSON file? 😓

1

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

Jsonc? Most tools already support comments.

1

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

Or - just don't use json.

1

u/HarryTerraCode 1d ago

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

1

u/TheZedrem 1d ago

ever heard of jsonc?

1

u/Dimencia 22h ago

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

1

u/NullSmoke 21h 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 20h ago

Sjson has it.

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

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

1

u/Desperate-Tomatillo7 20h ago

VSCode told met that there is JSONC.

1

u/MACMAN2003 18h ago

The genie is a tyrant!

1

u/RandomiseUsr0 18h ago

“c”:”fill your boots”

1

u/JackNotOLantern 16h ago

Schema, lol

1

u/Direct-Quiet-5817 10h ago

See, 🧞 JSONC 🧞

1

u/joan_bdm 2h ago

hjson you mean?

1

u/kingslayerer 1d ago

Maybe you should start using json schemas. You lazy

0

u/BadgerwithaPickaxe 1d 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

-1

u/necrophcodr 1d 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 1d 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