What are the advantages and disadvantages of json vs xml for ajax requests? Is there a difference in performance? i.e. are browsers able to process one format faster than the other?
9 Answers
In summary, JSON (which can be thought of a subset of JavaScript) is a lot leaner than XML. This has several positive side-effects
- JSON is smaller than corresponding XML
- JSON is faster, i.e. simpler syntax
->
easier parsing (faster parsing)
In my original answer to this question, my view of JSON was that of JavaScript, I considered it to be a close relative. But JSON is something independent and JSON.org does a great job of describing JSON. It's also provides a compatibility library for JavaScript that adds support for JSON.parse
and JSON.stringify
when not supported by browsers.
While eval
at the time (mid 2009) was used to evaluate JavaScript, it could also evaluate JSON, i.e. parse JSON, but it was considered unsafe, as it did allow arbitrary JavaScript to execute in its stead.
JSON just happens to be a very good fit for browsers and a natural way to evolve the platform due to its close relationship with JavaScript.
While XML might be considered to have better rigor due to the fact that you can type it, it is also those things that make it a lot slower (it is also a bit verbose in my opinion). But if this is something you really want, you should use it, XML is equally ubiquitous.
I will not go into a debate over dynamic or statically typed, but I will say this. It's really easy to add stuff on top of schema-free data and there are plenty of ways to do validation, regardless of schema or no schema.
-
-
1@John: You are mixing parsing with execution. They are two different beasts. Feb 25, 2009 at 10:05
-
1@Guillaume and dirkgently - JSON is a lot simpler to work with in conjunction with browser scripting. More suitable for pushing data on to the client. I like to think about JSON as specialized JavaScript because it will let me emit behavior from sever-side code which is not as tidy with XML. Feb 26, 2009 at 6:21
-
@MagnusHoff It's not wrong, it's just that
JSON.toJSON
andJSON.stringify
which originally wasn't supported by all major browsers is better in every way possible and more to the point. You should definitely use these like jQuery and many other frameworks do. But this isn't they way jQuery originally worked due to the browser issues at the time and jQuery was a big inspiration for many at the time, still is. Jun 14, 2012 at 6:46 -
I also took the liberty to rewrite my answer in response to these comments also to reflect maybe a more modern view of this question. I've also learned a thing or two these last years... Jun 14, 2012 at 6:56
One advantage of XML I havent seen sofar in the discussion is that XML can have schema. This is of great value in describing the structure of the XML. For simple data structure, JSON and a bit of text describing what you are doing is fine. When working with more complex data structures, or when the creator and the consumers of the data are not the same team, having a Schema can help the communication a lot.
Also, having a schema means that you can validate your data, which can be life saving when trying to debug complexe errors ...
-
schema is not always required strictly, especially working for NoSQL! Dec 19, 2015 at 12:52
You have in this article "The AJAX response: XML, HTML, or JSON?" a full debate on that topic:
XML
- Advantages
The most important advantage of XML is that it's the most easily readable format for other humans.
A secondary advantage is that XML has been around for quite a while and that many developers are already accustomed to it. - Disadvantages
The JavaScript required to insert the data into the HTML page is quite verbose.
JSON
- Advantages
The most important advantage is that JSON circumvents JavaScript's same-source policy, if you import the JSON file as a new<script>
tag. See Simon Willison's example for the gory details.
JavaScript does not allow you to access documents (be they XML or HTML) that come from another server. However, if you import a JSON file as a script tag you circumvent this problem, and any JSON data can be imported into any website. It depends on your business goals whether this is a Good or a Bad Thing, but right now it's the only data format that allows unrestricted access.
A secondary advantage is that scripts for JSON data are slightly simpler and slightly more in line with the rest of the JavaScript language than scripts for XML data. - Disadvantages
The most important disadvantage of JSON is that the format is very hard to read for humans, and that, of course, every single comma, quote, and bracket should be in exactly the correct place. While this is also true of XML, JSON's welter of complicated-looking syntax, like the }}]} at the end of the data snippet, may frighten the newbies and make for complicated debugging.
From the comments, JSON is considered faster to process than XML.
-
There's no reason stuffing XML into the DOM has to be hard, if the design on the server side takes that behavior into account. A little design work on the server side can greatly reduce the browser side effort. May 19, 2012 at 14:27
-
1Such a ridiculous point; JSON is hard to read for humans? Are your seriously still using this as an answer? JSON is incredibly more easy than XML to read for humans; universal fact. I can't believe how idiotic that statement is! Nov 2, 2015 at 12:36
One advantage of XML that hasn't been mentioned yet is that it is possible to use XSLT to extract data from an XML document using arbitrarily complex XPath expressions. I can't see a way to do this in JSON. How would you find all objects in a JSON document that contain a "currency" field as a direct descendant, irrespective of where the objects occur in the structure? In XML with XSLT/XPath this is very easy.
However, doing this in the browser comes at a price. You will run into browser-specific quirks, and it will probably be slower and more memory intensive.
-
1"How would you find all objects in a JSON document that contain a "currency" field as a direct descendant, irrespective of where the objects occur in the structure?" The same way an XSLT transformer does, by walking the structure and finding them. Just because there isn't a well-known JSON transformer doesn't mean that there couldn't be, or that it would be less-useful than the XML version. Dec 26, 2010 at 11:57
While I like Json and would recommend it, I think that there is no fundamental difference between optimal processing speed. Differences between libraries and platforms are more significant: a good xml parser is faster than bad json parser and vice versa. So usually performance of formats themselves is not a big factor. Both can be lighting fast (Java has a few good parsers for both, for example, other languages problably too).
As to compactness, that depends on type of data, Json is often bit more compact, but not radically so. Except if you have lots of arrays/lists, where json notation "{ ... }" is much more compact than xml tagging (unless you use white space if possible, like "1 2 3 4").
The link dirkgently provides has a good synopsis of the differences (scroll to the bottom). Important points are:
JSON: smaller, can represent all unicode characters (xml can't in UTF8, at the least, by its own spec). Its also trivial to make use of it in AJAX applications (because of javascript's eval() function), especially more advanced features like jsonp (responses invoke callbacks).
XML: Has lots of tools to make querying its structure easy. Since its older and more established, it also has more tool support in general.
In general, they can accomplish the same things (you could do jsonp through xml, it would just require manually parsing out the callback).
-
I don't agree with your statement that XML cannot represent all Unicode characters. You simply set the encoding to Unicode ("UTF-16"). See how: w3schools.com/xml/xml_encoding.asp Jul 18, 2011 at 9:55
-
Wow, this is from awhile ago :). Aren't there explicitly forbidden characters in XML? I seem to remember some overlap between those and utf8 for sure, not sure about utf16. I've updated my answer to reflect that its in, at the least, utf8. Jul 18, 2011 at 18:41
You may want to read JSON: The Fat-Free Alternative to XML. With JSON you can fool around with callbacks to and fro between the source and the destination of a request and actually use it painlessly in your existing Javascript code.
i think another advantage of Json is that it doesn't use <> brackets and you can put html code in it without a lot of confusion.
JSON is lightweight, AJAX friendly (considered as subset of Javascript) and easily serializable. XML do all this but after drinking some milk.