Why does JSON have commas?
As it happens, I already wrote a post on why Commas are Bad. We did not arrive at any specific conclusion apart from commas maybe being worth rethinking and replacing with some other things.
Among all the languages and formats that could do with fewer of them... there is one that most definitely stands out. In the sense of... in this format, commas are not just a result of some misguided design decision, with lingering, unpleasant effects, but a well-honed instrument of torture, clearly existing only to make your life more hellish.
It's JSON.
Derived from the array and dictionary literals of the programming language that was famously designed in 10 days (before suddenly taking over the internet), JSON has become ubiquitous; its simplicity is a great alternative to the arcane complexities of XML, the unreadability of many binary formats, and the... um... oddly alien lispyness of S-Expressions. The format is really simple! Look:
{
"name": "小明 (xiǎo míng)"
"age": 25
"isStudent": true
"grades": null
"contact": {
"email": "xiaoming@example.com"
"phone": "123-456-7890"
"address": {
"street": "123 Main St"
"city": "Beijing"
"zipCode": "100000"
}
}
"interests": ["编程 (biānchéng)" "reading" "travel"]
"achievements": [
{
"title": "First Place"
"year": 2022
"details": {
"competition": "Coding Challenge"
"score": 98.5
}
}
{
"title": "Second Place"
"year": 2023
"details": {
"competition": "Math 学习 (xuéxí)"
"score": 95.2
}
}
]
"favoriteNumbers": [7 42 99]
"personalQuote": "学习 (xuéxí) is a lifelong journey!"
"generatedBy": "Claude Sonnet 3.7"
}
Now... have you noticed anything though? We... might have gotten rid of... something.
Unlike the speculation in Syntax is Overrated though... the box of code above is not just kinda suggestive of some changes we could make. No. This, above, is an actual working file format.
You take JSON. You throw out the commas.
Think about it! Lists already have space-separated elements. Dictionaries? consume a key, expect a ":", consume the value. Continue. Still easy to parse.
Coming from this direction, the addition of commas feels like an evil plan to have more syntax errors, with no obvious benefit.
To be fair, dictionary one-liners might be a tiny bit less easy to read:
{"key1": "value1" "key2": "value2"}
... but you still have the colons to easily find which ones are the keys & which ones are the values. Also, why not add optional commas? Treat it like whitespace, basically! (... just like line-ending semicolons are optional in JavaScript!)
How come no one else...
As it happens, the issues with commas in the original JSON format has caught the eye of multiple people. In fact, there is a new format (... amendment?) called JSON5 which aims to fix... some of them.
Sadly, they are not ambitious enough.
Specifically, JSON5 allows the use of trailing commas in a way similar to how other programming languages use semicolons. So, instead of having to inject a useless separator after each element, except for the last one (which is fairly annoying if you're moving / diffing lines around), now you can follow the simpler rule of inject useless separator after each element. Which is nicer.
{
"title": "Second Place",
"year": 2023,
"details": {
"competition": "Math",
"score": 95.2,
},
},
And yet.
The presence of Chinese characters in this post is not a coincidence because nothing is ever a coincidence; this shall be counted under "odd side effects of utilizing AI systems". There might be more posts on this later.