Is JSON better than XML?
Use JSON, not XML. You hear it all the time these days. JSON is so much better. It must be because everyone is saying it, right? I’m starting to think not.
Fellow Yahoo Douglas Crockford, creator/inventor/founder/father of JSON, says that JSON is better than XML. He says it whenever he can (most recently in his post Does XML have a future on the web?). It is, of course, natural that he would say that JSON is better than XML; every parent thinks their kid is adorable and quite possibly the smartest kid ever. I’m just not sure his hypothesis is based in fact.
What really makes one data interchange format better than the other? As long as it serves the needs of the developers using it, then the format can at least be said to be adequate. The primary usage of JSON is for data delivery between browsers and servers. Even though it can technically be stored in files and the like, JSON is rarely used outside of the web environment. XML can also be used for data delivery between browsers and servers but also is stored in files and used to extract data from database. For this comparision, I’ll just consider the browser/server usage.
Syntax: JSON syntax is quite light and definitely less verbose than XML, with all of its start and end tags. When it comes down to pure byte-size, JSON can represent the same data as XML using fewer characters.
Weight: Since JSON syntax requires fewer characters, it is lighter on the wire than XML. The question is if this really matters. Any large data set is going to be large regardless of the data format you use. Add to that the fact that most servers gzip or otherwise compress content before sending it out, the difference between gzipped JSON and gzipped XML isn’t nearly as drastic as the difference between standard JSON and XML.
Browser Parsing: On the browser, there’s no such thing as a JSON parser. However, there is
Server Parsing: On the server, parsing is just about equal between JSON and XML. Most server-side frameworks have XML parsing capabilities and many now are starting to add JSON parsing capabilities as well. On the server, these parsers are essentially equal, parsing a text format into an object model. JSON holds no advantage over XML in this realm.
Querying: This is where XML really shines. Using XPath, it’s possible to get direct access to a part of multiple parts of an XML data structure; no such interface exists for JSON. To get data from a JSON structure, you must know exactly where it is or else iterate over everything until you find it.
eval(), it opens up a huge security hole. The
So what do you think? Is JSON better than XML?