89 points · by mariuz · 6y ago · 167 comments
Per Douglas Crockford, the creator of JSON, comments were an anti-feature:
> I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability. I know that the lack of comments makes some people sad, but it shouldn't.
> Suppose you are using JSON to keep configuration files, which you would like to annotate. Go ahead and insert all the comments you like. Then pipe it through JSMin before handing it to your JSON parser.
* https://web.archive.org/web/20120507093915/https://plus.goog...
Discussion on the post (2012):
* https://news.ycombinator.com/item?id=3912149
Remember: JSON was designed primarily as a data exchange format between computers.
I think this is a bit of 'over reach' in what JSON was intended to do, so it's perhaps not surprising that there may be some things 'lacking' for that purpose.
They're really bad for a data interchange format though, because people inevitably start putting important data in them and you end up with two different ways to write strings, one of which isn't supported by every parsing library.
Thus each discussion of JSON ends up being two groups talking past each other, the people using it as a configuration format who lament the lack of comments, and the people using it as a data interchange format who celebrate it.
The solution: since it's too late to add comments now, don't use JSON as a configuration format.
Any feature or tool can be abused and misused. Comments are useful for configuration files, period.
For data interchange formats comments do presents major problem.
I don't have a single competitor to recommend, but JSON5, TOML, JSONC (mentioned in a sister comment), or something else along those lines might be better. I'd probably just go with whichever of those is popular in your community.
The core project implements a JavaScript module [1] but it looks like there are also experimental JSON5 libraries for Go [2], Python [3], Ruby [4], and Rust [5].
[1]: https://www.npmjs.com/package/json5
[2]: https://github.com/yosuke-furukawa/json5
[3]: https://pypi.org/project/json5/
[4]: https://github.com/bartoszkopinski/json5
I noticed that JSON5 doesn’t include a Date type. There have been discussions about this in both the JavaScript [6] and spec [7] repos but with no resolution yet.
<script src="//unpkg.com/json5/dist/index.min.js"></script>
to every HTML page as extra JavaScript instead of a native browser implementation.The litany is not short and I'm sure I've overlooked a couple.
But if you add too many features, it may end up like XML.
For numeric types, if you handle integer larger than 2 billions or floating numbers where the precision is critical (that's where json fails), these can and should be represented as string. I don't think there is any common format that guarantees large numbers and/or arbitrary precision to be encoded and decoded properly across languages and platforms.
Perhaps CDDL:
This allows trailing commas and comments. This can also reads strict json of course. Libraries are available for all popular languages.
However, JSON is hitting the same limitations and problems that XML faced, and is following in their shoes (namespaces, schemas, x/jpath, implementation drift across libraries, etc).
And is it following in XML's shoes? I've not had to work on or integrate with any systems that did this Enterprise™ silliness either.
Now granted, I'm fairly separated from the world of IBM and SAP type companies, and I'm sure there's all sorts of unholy abuse of JSON going on in a dark server room somewhere, but the answer in most cases is just to choose technology stacks that don't do that to you. These problems are generally self-imposed rather than imposed by the technology.
I think that's why JSON won: it was first and foremost and engineer-driven standard as opposed to one that incubated inside a bunch of enterprises before being dropped on an unsuspecting world.
Yes. Some folks, even at my relatively small company, are using JSON and also pushing for extensive schema use.
JSON - just like XML - allows you to use its vanilla form. But the additional tools are available if/when you need them, because the need exists.
S-expressions have the advantage of not containing the redundant object type (there's no need for an object, map or dictionary when one has alists or plists), and the even greater advantage of elegantly representing code.
Also they are easier to write parsers for.
I can see why Lisp style syntax is bad for human comprehension. It’s because of the lack of an explicit delimiter, like a comma. I don’t think human eyes are very good at using a single space character, as a delimiter.
And with this format, you’d have to agree upon the data exchange structure. There would have to be a master key somewhere, maybe as the header. Albeit, this format is far more efficient than XML.
But, XML probably won out because it was explicit, and allowed multiple levels of nesting, but at the expense of overly wordy delimiter tags.
And JSON made it simpler, by forcing it to be a lightweight key-value pair.
It’s too bad something like this didn’t take root and become more popular. This might be a very useful advanced data exchange format for some scenarios. Although the headaches and problems associated with trying to understand, and work with it, might far outweigh its efficiency benefits.
Unless of course you'd put the comments in the data itself, but that's kind of ugly.
JSON is normally "good enough" as a general wire interchange format, and it's human-readable and whoever comes after me will already be familiar with it. But if there's a time when it's not good enough and its failures are in expressiveness, I'd totally consider JSON5 or some other alternative instead.
Plenty of software that uses regular JSON also strips unused data when writing back to a file it read because internally it transforms the document into a data structure with no space dedicated for unknown data.
It would be up to the software, or a very strict standard or library, to preserve unknown/unused data.
{
"_COMMENT": "default; override with --s3-bucket",
"s3_bucket": "foo-bar"
}JSON was first designed as a data exchange format between computers (that was lighter weight than XML (which was lighter weight that SGML)), so that fact that it's being used in persistent fashions is getting away from its primary purpose.
[1, 2, 3, 3.14159, "comment: Pi!!", 4, 5, 6, 7, 8]
over: [1, 2, 3, 3.14159 /* Pi!! */, 4, 5, 6, 7, 8]I suspect that a lot of the popularity is piggy-backing on the popularity of JavaScript.
But what the heck is .CSV doing on an incline?!
It is still the most common interchange format for tabular data. Tabular data is too verbose/unreadable when represented in standard JSON. Databases and Data Science are also on the rise; .csv continues to ride this wave.
The downside is that you can’t process a row at a time.
Once you're using a binary format, you might as well use something like parquet or hdf5 (or ASN.1 I suppose) that give you other goodies in addition to airtight data/organization separation.
I always thought it was chosen since it was a seemingly sensible delimiter at first glance and then you only realize it's less useful well after you see others using your spreadsheet app for storing complex strings.
T̵h̵e̵ ̵l̵a̵t̵e̵s̵t̵ ̵v̵e̵r̵s̵i̵o̵n̵ ̵o̵f̵ ̵J̵S̵O̵N̵ ̵w̵a̵s̵ ̵a̵p̵p̵a̵r̵e̵n̵t̵l̵y̵ ̵r̵e̵l̵e̵a̵s̵e̵d̵ ̵l̵e̵s̵s̵ ̵t̵h̵a̵n̵ ̵a̵ ̵y̵e̵a̵r̵ ̵a̵g̵o̵.̵ [EDIT: I was mistaken: the latest standard was RFC 8259. However, this was still published on 2017-12-13, after the article was written.] There have been three different RFCs alone, all defining JSON. (And let’s not even get into the thing called JSON5.)
* https://tools.ietf.org/html/rfc8259#appendix-A
Not very Earth shattering revisions IMHO.
<complexType name="MyMethodResponse">
<sequence>
<element name="A" type="string" />
<element name="B" type="string" />
</sequence>
</complexType>
and you later add a "C", existing clients will fail, so you will have to create a new method along side the old one to remain compatible.Similar to defaulting to error-ing on extra tags, I never got the point of making everything an element and adding extra redundant attributes. That's really just the result of XML that's automatically generated.
Like the `<element... type="string">` in your example of classic XML, why not just use `<a>3</a>` or `<a int="3"/>`. XML proper styles still suggest the classic long/autogenerated form. Writing XML in html5 style like `<my-a data-value="3" />` makes it much friendlier. Or as I tend to use it for various internal protocols to wrap multiple CSV data in a file:
<some-data type"csv" columns="A, B, C">
1.3, 3.4, 5.6
1.3, 3.4, 5.6
</some-data>
Technically That's really just html5 in a file I guess. :-) But html parsers also don't tend to complain about extra tags too.I recently used GraphQL on a project and it had some nice advantages. I love the idea of protocol buffers but have never had the chance to use them in anger. But if I'm honest, the boring option is JSON and it is what I would use for just about any API I had to expose nowadays.
Previous discussion: https://news.ycombinator.com/item?id=17832936
I had to add XML and CSV, because "nobody would integrate with a JSON API"
In 2011 we moved from an PHP app to an SPA where JSON came in handy.
It's a format that still bears excessive decoration (what's the purpose of quotes around field names? what are all those commas for?) yet it's limited in the types of data structures that it's able to express (natively). I'm not particularly fond of Clojure specifically but a format like EDN would have been superior in just about every way.
The datastructure complexity being limited is also a pretty significant key to its success. More complex datatypes means greater chances for JSON handling libraries to lack compatibility.
The only substantial shortcomings of JSON I see are shortcomings associated with any textual serialization format. Optimizing for human readability in a use case that's 99.99% of the time not read by a human.
That's literally how low you have to go.
"The only substantial shortcomings of JSON I see are shortcomings associated with any textual serialization format. Optimizing for human readability in a use case that's not 99.99% of the time not read by a human."
There are a few other shortcomings that are reasonably substantial/significant, but yeah, that's the gist of the problem.
{"time":"2020-07-22T10:59:14.95406-04:00","message":"{\"level\":\"debug\",\"module\":\"system\",\"time\":\"2020-07-22T10:59:14.953909-04:00\",\"message\":\"Running MetricCollector.Flush()\"}"}
this is a very moderate example of what I deal with daily, all because JSON includes quotes around fields.
https://amzn.github.io/ion-docs/
Superset of JSON with many extra features that people in the comments here desire from their data-language, such as support for comments, timestamps and s-expressions.
So everyone gets to suffer for his beliefs.
Thankfully JSON came along and got back to the simplicity of early XML and XML-RPC and stayed there.
Deserialization and remote code execution vulnerabilities all over the place. That was brutal.
Who thought this was a good idea to pass arbitrary function names and arguments for the remote servers to resolve and execute blindly? The regular vulnerabilities in the XML parsing libraries themselves was the nice cherry on top.
I'm not sure who would would to that but I certainly didn't. Ultimately XML-RPC is no different from REST/JSON except it's in a different format. What you did with that format is a totally different issue.
My favorite quote about XML-RPC (from the plan9 people):
Some part of me desperately wants to believe that XML-RPC is some kind of elaborate joke, like a cross between Discordianism and IP Over Avian Carriers
I have exactly the same feeling regarding the JSON "protocols" and whatnot.
Maybe something like TOML? It's very human-readable and simple. But it's not a very good serialization language.
“The good thing about reinventing the wheel is that you can get a round one.”
XML is a mess and a chore to work with (at least in any language that isn't Java I guess, but even then).
Yes, it has some rough edges. Yes it could be better.
But overall it's good. Not too complicated and not too hard. Works fine for most stuff.