4 ms·
Why does JSON have commas?
- laurentlb 2y agoIf it's about readability, you could have a rule "either comma or line break".
- 01HNNWZ0MV43FF 2y agoAt this, the me was enlightened
- nurettin 2y agoMakes sense. A PEG would probably be the same length as the original json format.
- leshenka 2y agoWe can throw away colons too. Dictionaries will be sequences of “key””value””key””value” Just because we can throw away redundancy doesn’t mean we should
- tarvaina 2y agoThis is how Clojure does it. https://clojure.org/guides/learn/hashed_colls#_creating_a_literal_map https://clojure.org/guides/learn/hashed_colls#_creating_a_li...
- EGreg 2y agoAnd iOS programming makes you fill dictionaries with alternating k,v,k,v,nil
- frizlab 2y ago*The Swift language (not only iOS programming at all) *it is one way of initializing a dictionary. Literal way has a closer to JSON form. EDIT: Oh you seem to be talking about ObjC it seems! I did not see the nil. Literal way is still more akin to JSON though.
- jonplackett 2y agoKinda answers its doe question though To be fair, dictionary one-liners might be a tiny bit less easy to read: {"key1": "value1" "key2": "value2"} Not just a bit less easy, a lot less easy
- ghusto 2y agoHe also accounts for that immediately by saying they should therefore be optional.
- t3rra 2y agoa hugely uneasy to read indeed. I feel wasted time to read the article. So horrible.
- yonixw 2y agoThis. The article suggestion will make parsers need lookahead support instead of simple state machine. Is "key1" array of ["value1", "key2"] ? or string "value1"? We can't now until we find ':'.
- frizlab 2y agoHuh? The article does not suggest dropping the [] or did I miss something?
- solids 2y agoThe main reason behind JSON widespread usage is human readability.
- croes 2y agoWhat exactly is the problem? By that logic one could argue the whole JSON format is bad because of all the key name redundancy.
- tracnar 2y agoOr go even further and you get yaml?
- ajuc 2y agoYAML: when you wished json made you count spaces to avoid syntax errors I don't even mind indentation for nesting, but this key: value vs key:value is EVIL.
- fnordsensei 2y agoIn EDN[1], which is to Clojure as JSON is to JS, commas are whitespace (as it is in Clojure itself). 1: https://github.com/edn-format/edn https://github.com/edn-format/edn
- CWIZO 2y ago> Coming from this direction, the addition of commas feels like an evil plan to have more syntax errors, with no obvious benefit It helps to learn your history before you criticise something and claim it is useless. JSON is the way it is mainly because it is just JavaScript and that meant that every browser in the world already supported it before JSON was even invented. It is THE reason why it is as popular as it is.
- jeroenhd 2y agoRelying on the inherent JavaScript compatibility hack has caused as many problems as it has solved. Plus, the additional of trailing commas made a mess of things once more, forcing developers to let go of the direct compatibility with many versions of JavaScript engines and redirecting them to specialised JSON APIs instead. The easy solution would be to go the JavaScript route and make commas optional, like JavaScript does with semicolons. You could even introduce vague and easily forgotten rules about commas being inserted between oneliner dictionaries. I think the question "why" is easily answered, but "why should it still" is more difficult.
- eastbound 2y agoJokingly, why does Javascript have the semi-colon “;”
- jeroenhd 2y agoThere are a few minor, uncommon edge cases, probably only encountered by minimisers, where automatic semicolon insertion may cause unintentional behaviour, so semicolons are required. One of the specs; https://262.ecma-international.org/7.0/#sec-rules-of-automatic-semicolon-insertion https://262.ecma-international.org/7.0/#sec-rules-of-automat... (link from an earlier comment of like) spells it out in detail.
- EGreg 2y agoanyone actually use comma-first syntax? It lets you easily move lines around, too: {"foo": "bar" ,"baz": "hehe" ,"yes": "aligned" ,"better": "this way"}
- scottmf 2y agomore easy how?
- AlchemistCamp 2y agoNot the top one!
- nkrisc 2y agoThis has the same number of lines that can’t be moved freely as comma at the end.
- EGreg 2y agoBut visually easier to check Also you can put the last brace on its own line
- nkrisc 2y ago> But visually easier to check I’ll agree with that, since they’re all vertically aligned. > Also you can put the last brace on its own line You can always put the first and last brace on their own line in most languages whether the commas are at the front or end.
- EGreg 2y agoBut in the comma-first case that lets the last line be movable, that was my point basically.
- sham1 2y agoHaskell culture tends to do this kind of stuff for records, FWIW. Of course, in this case, it might even make sense to render this in a way such as the following: { "foo": "bar" , "baz": "hehe" , "yes": "aligned" , "better": "this way" } Because as another person already said, the first line isn't movable, and the last line wasn't either. The first key-value pair does look a tad strange because it doesn't have anything before the key, but nevertheless, now changing things is even easier. Admittedly, it would look nicer if we could have trailing commas, though. Oh well, we have YAML for extra readable record syntax, even with how horrible YAML can get in reality.
- smackeyacky 2y agoReadability matters and humans like that. Otherwise XML would have won. https://guidetogrammar.org/grammar/twain.htm https://guidetogrammar.org/grammar/twain.htm
- jeroenhd 2y agoI don't find commas at the end of line adding any more readability than avoiding them, except for the oneliner counter case. JavaScript has optional separators (semicolons) with some rules to avoid ambiguity. Not everyone like it (I use semicolons in JS unnecessarily because I can't be bothered to learn the three edge cases they're not optional). I think there's something to be said for "commas are optional except in oneliners". Languages like Go describe structs without commas. It took me some getting used to, but it's really not more or less readable in my opinion.
- gregopet 2y agoIf readability was the only criteria perhaps YAML deserves to win. For some data XML is more readable, its complexity is the bigger problem. People went all in with things like XSLT and namespaces, Java became the "language that transforms XML into stack traces" for no good reason and complex XML monstrosities were considered normal so when something simple came along it was a much needed breath of fresh air (we were even prepared to overlook its lack of comments). If it wasn't JSON it would have been something else, I'm sure.
- smackeyacky 2y agoWe are trained from a very young age, as a rule, to see the comma. The semicolon, not so much. Semicolons are unusual: they are not part of story telling; they are from a forgotten grammar which is hardly used.
- card_zero 2y agoPooh had wandered into the Hundred Acre Wood, and was standing in front of what had once been Owl's House. It didn't look at all like a house now; it looked like a tree which had been blown down; and as soon as a house looks like that, it is time you tried to find another one. What do you mean, not part of story telling? There are 64 semicolons in The House at Pooh Corner; and by the way, nearly all of them are followed by conjunctions.
- paulmooreparks 2y agoI wrote my own language mainly to get rid of commas and colons, and I added comments while I was at it. https://github.com/paulmooreparks/Xfer/tree/master/ParksComputing.Xfer.Lang https://github.com/paulmooreparks/Xfer/tree/master/ParksComp...
- tommy_axle 2y agoNeat. Did you have any thoughts on json5 or hjson?
- jgyter 2y agoWhy? What does he actually gain? The small convenience of skipping a comma in the one specific case where a human is reading pretty-printed JSON? It's an adamant No from me.
- ghusto 2y agoIn configuration languages such as HCL, having to account for the fact that there will not be a comma for the last element becomes unnecessarily difficult.
- dazhbog 2y agoWhen it comes to serialization or sending data over the wire (non http), I prefer relaxed json (no quotes on keys e.g. {a:1,b:"hi"} ). It keeps the message readable and it can be re-assembled on the server. That is if MessagePack or CBOR are not an option.
- antirez 2y agoThe problem is not commas, but the fact the specification does not tell you MUST, in a conforming parser, handle extra commas at the end. While commas are not very useful in this case, they have some value because: 1. They help humans parse the JSON object: we are helped by structure. 2. They add redundancy of information and allow for simpler spotting of syntax errors. What JSON really needs is to extend its syntax to allow strings that are prefixed length, without the need of a binary protocol. "foo": 6="foobar". Otherwise JSON forces you to parse byte by byte, which is extremely inefficient for large amounts of data.
- conaclos 2y agoI kinda disagree with the conclusion. Punctuation is important to humans, it makes it more parsable/readable. I just accept that in a multi-line context they are not so useful, because the newline acts like a kind of punctuation. So I could make them optional and provide some recommendations: use them in a single line, and don't use them when a newline follows.
- geor9e 2y agoThese JSON versus YAML versus whatever discussions seem to mainly be arguments from people hand-coding it in grey ASCII with no tools. IMO the ASCII itself just needs to be machine-readable, because humans have visualization tools to parse it and give a pretty view. I'm not Rain Man.
- EPWN3D 2y agoCriticizing JSON for needless commas with a post full of needless ellipses.
- librasteve 2y ago{ "interests": ["编程” "reading" "travel"] } is written { :interests<编程 reading travel>, } in raku literals, quite a lot less wiggling of the pinky than the authors “maximal” proposal yeah raku : perl without the line noise