Skip to content(if available)orjump to list(if available)

Why does JSON have commas?

Why does JSON have commas?

60 comments

·April 6, 2025

CWIZO

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

sebazzz

It was just a matter of:

    var myObject = eval(httpResponseBody)

Of course, this is vulnerable to all kinds of issues so we got JSON parser, and later JSON became part of the Web API.

cosmotic

I remember the infinite loops stuffed into JSON results to prevent this sort of thing.

Xmd5a

If it is "just JavaScript", why the lack of comments ?

dharmab

Comments were intentionally excluded to prevent the use of parsing directives.

(Sadly, Crockford's post about this reasoning was on Google Plus and is no longer online.)

sargstuff

{comment : "your comments here!"}

{copyright comment : "JSON doesn't require/enforce specific comments is a good thing" }

{comments : "JSON parser is not an 'comment' editor! }

Don't know whtat the google plus article covered, but Crockfor's comments : https://www.linkedin.com/posts/douglas-crockford-724600109_j...

Tomte

The author knows that. Fourth paragraph.

pwdisswordfishz

If the author knows that, why even bother with writing a whole "what should have been" article? It could not have been any oher way.

eastbound

Jokingly, why does Javascript have the semi-colon “;”

jeroenhd

There 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-automat... (link from an earlier comment of like) spells it out in detail.

Timon3

While these edge cases are fairly rare in everyday JS codebases, Typescript adds some, e.g. inline type casts for function calls:

    const foo = someFn()
    (foo as Bar).doSomething()

sargstuff

Yet another example/demo of "Use the source!"

There is nothing in the JSON spec that prohibits pre/post non-json parsing search / replace 'end of line' marker with one or more commas.

JSON spec just tells one what the JSON parser expects, not what the end user needs/wants. (GPT-JSON not withstanding)

michaelcampbell

> > ... feels like an evil plan to have more syntax errors, with no obvious benefit

> ... before you criticise something and claim it is useless.

These 2 statements feel quite far apart.

jeroenhd

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

randunel

Making things optional means you'd end up with the csv standard mess on json, let's not!

watwut

The easy solution is horrible solution. Pretty much all the projects set semicolons as mandatory for a reason.

leshenka

We 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

EGreg

And iOS programming makes you fill dictionaries with alternating k,v,k,v,nil

frizlab

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

antirez

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

fnordsensei

In 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

jonplackett

Kinda 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

yonixw

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

Huh? The article does not suggest dropping the [] or did I miss something?

ghusto

He also accounts for that immediately by saying they should therefore be optional.

t3rra

a hugely uneasy to read indeed. I feel wasted time to read the article. So horrible.

jgyter

Why? 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

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

smackeyacky

Readability matters and humans like that. Otherwise XML would have won.

https://guidetogrammar.org/grammar/twain.htm

jeroenhd

I 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

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

Timon3

YAML seems easier to read, but for me it's much harder to actually read properly since so many things are ambiguous. JSON is very explicit - you always know the data type you're looking at. YAML has a bunch of special cases that you have to remember, and these frequently lead to bugs.

The best example are boolean values. With JSON, you have `true` or `false`. With YAML you have all of these: y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF

smackeyacky

We 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

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

geor9e

I get the argument, but I can't imagine a double-blind study of children or adults of any age that would find they see a semicolon versus a comma at a different rate. If anything, the reverse argument works; the semicolon is more obvious because it's so unusual.

solids

The main reason behind JSON widespread usage is human readability.

croes

What exactly is the problem?

By that logic one could argue the whole JSON format is bad because of all the key name redundancy.

geor9e

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

tracnar

Or go even further and you get yaml?

ajuc

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

conaclos

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