Most active commenters
  • xvolter(4)

←back to thread

Fixing JSON

(www.tbray.org)
139 points robin_reala | 12 comments | | HN request time: 1.062s | source | bottom
Show context
outsidetheparty ◴[] No.12327880[source]
Shameful confession: when I was first introduced to JSON, I was convinced it would go nowhere. "XML already does everything JSON does! And there's no way to differentiate between nodes and attributes! And there are no namespaces! And no schemas! What's the point of JSON?" And a couple of years later I looked up from the tangled mess of XSLT I was working on to discover that the rest of the world had moved on.

JSON is just javascript, and it leaves out everything else. That's its entire reason for existing, and it caught on because that's all that 99.44% of anyone needed.

Timestamps you can add today, without changing the protocol; just put them in your data if you need them. So I'm not sure what he's even proposing there.

Schemas: OK, he doesn't like JSON Schema's anyOf. Fair enough. There's no real proposal here for how to fix it, so not much to say here.

Replacing commas with whitespace sounds to me like replacing a very minor irritant with a constant full-body rash. Stuff like his example of "IDs": [ 116 943 234 38793 ] would lead to far more confusion and errors than the occasional stray trailing comma.

So I guess I pretty much vote no on this one thanks for asking

replies(9): >>12327976 #>>12328071 #>>12328074 #>>12328283 #>>12329722 #>>12329776 #>>12330073 #>>12330517 #>>12334062 #
1. xvolter ◴[] No.12328283[source]
I was thinking along very similar lines when reading the article by Tim Bray. JSON is designed for JavaScript compatibility, I almost stopped reading when he said:

> They’re inessen­tial to the gram­mar, just there for JavaScript com­pat­i­bil­i­ty.

The entire article goes on to suggest extremely terrible solutions to the problems he is pointing out.

In order:

Commas: If comma's are an annoyance, is that to mean there is no annoyance with XML markup? You can forget a closing tag just as easily, probably more easily. Just get a JavaScript or JSON linter. Fixed. If you are really annoyed by commas, move to YAML or another format.

Datetimes: Just store your dates in a standardized timezone and format, how about [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). You can also use a [JSON-Schema](http://json-schema.org/) if you really want.

Schemas: There's lots of potentials here. However, again, if you're going to change JSON so much, why not just pick another data format that has better schema support.

replies(5): >>12329120 #>>12329744 #>>12329802 #>>12330331 #>>12334990 #
2. sopooneo ◴[] No.12329120[source]
For datetimes, do you mean that they be set as strings in the standardized formats such as specified by ISO 8601? Like:

{"item": "lawn chair", "order-timestamp": "2016-08-21T00:58:47Z"}

replies(1): >>12329806 #
3. rat87 ◴[] No.12329744[source]
> JSON is designed for JavaScript compatibility,

Javascript compatibility is the least important part of json, lots of people use json with python or ruby or rust or java or c for things that will never interact with javascript, and anyways you don't want people to parse json with eval or confusing json with things that include string concatenation or function calls adding non javascript syntax helps make that clear.

There is a good reason to not change JSON(backwards compatibility, updating parsers, confusing formats, ect.), since you're not planning on parsing javascript with eval there is no good reason why any changes made in JSON 2.0 have to conform to a subset of javascript.

replies(1): >>12329818 #
4. anon1385 ◴[] No.12329802[source]
>JSON is designed for JavaScript compatibility, I almost stopped reading when

Why does Javascript compatibility matter in 2016? The original idea for JSON might have been to parse untrusted data using js eval() but I'd hope that nobody is doing that anymore.

replies(1): >>12329829 #
5. xvolter ◴[] No.12329806[source]
Yes, that would be a strongly recommended way. I could see using a unix timestamp as a numeric field as another way.
6. xvolter ◴[] No.12329818[source]
I think if breaking changes are made, there is no point, it would not be called JSON 2.0; it is an entirely new data format. When there are hundreds to thousands of alternatives, trying to "fix JSON" isn't the solution. JSON works because it is simple and easy to follow, it is robust and flexible, and has no restrictions. There is no good way to store dates in XML or many other formats, without the use of Schemas, and the syntax changes are minimally beneficial. It wouldn't save a lot of bandwidth with gzip, and the text data likely doesn't add up to a lot of disk space when compared to the data being stored; so removing commas which make parsing JSON easy and increases its adoption and usage, just so some people can have less linting errors doesn't seem like a large benefit.
7. xvolter ◴[] No.12329829[source]
I don't believe the original purpose of JSON was to eval untrusted objects, since that would always have posed a security risk. It did however offer a very easy standard way to share and parse data back in 1999. In 2016, I believe continued backwards compatibility is a necessity; otherwise you are just creating a new data storage format. So removing commas doesn't seem like an acceptable improvement, especially when the recommendation comes out of a necessity due to human error while hand-editing. JSON is incredibly easy to read and write by hand, in comparison with XML it is far easier to parse and traverse through code do its significantly simplified structure. If however, you need more functionality, switch to a different format; why bother trying to mangle or force JSON to work where it doesn't. There are other technologies and standards out there, use them.
replies(1): >>12330687 #
8. JimDabell ◴[] No.12330331[source]
> If comma's are an annoyance, is that to mean there is no annoyance with XML markup? You can forget a closing tag just as easily

That's not the same thing at all. The commas in JSON are equivalent to the whitespace between attributes in XML. A missing closing tag in XML would be equivalent to forgetting to close an object literal or array in JSON. So no, there's no equivalent XML annoyance to what he's asking for, XML is already fixed in the manner he suggests.

9. macspoofing ◴[] No.12330687{3}[source]
>It did however offer a very easy standard way to share and parse data back in 1999.

Not 1999. 2004. JSON wasn't on anybody's radar in 1999.

>JSON is incredibly easy to read and write by hand, in comparison with XML

Maybe for small shallow objects (and for those XML is also quite readable). Once size or complexity get a little higher, you're done.

replies(1): >>12330748 #
10. hutzlibu ◴[] No.12330748{4}[source]
> Once size or complexity get a little higher, you're done.

And what is better than JSON, when it gets complex?

replies(1): >>12330844 #
11. macspoofing ◴[] No.12330844{5}[source]
I meant you're done with reading and writing it by hand.
12. Mikhail_Edoshin ◴[] No.12334990[source]
The issue is not about forgetting something. XML elements are self-contained, you don't need to separate them, thus you don't need to known the context. If you generate XML programmatically, each function can just push its object into the stream and be done. With JSON you need to know if your object is last or first at this level of hierarchy, so you know if you need to insert a comma or not.

Commas were meant to be used in code that you actually type. Using them as separators in a format that is meant to be produced and consumed by computers is clear, simple, and wrong.