views:

2609

answers:

7

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?

A: 

You may want to read this. 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.

dirkgently
The link is broken (at least for me...)
Ido
@ido: try json.org
dirkgently
+2  A: 

In summary, JSON (which can be thought of a subset of JavaScript) is a lot leaner than XML. This has several side-effects, e.g. JSON is generally smaller than a XML document and there for faster to work with.

JSON can be parsed more efficiently because it can be parsed as JavaScript, which the built-in eval() function will do for you. It's considered unsafe to allow remote JavaScript to execute in this manner, but for the most part it's really perfectly fine. The paranoid would apply a regex on the input to escape potential harmful input, it makes things a little bit slower, but it's a lot faster than a XML document (which comes with a sizable overhead).

A big difference between JSON and XML (or HTML to be specific) is that HTML represents static content while JSON in conjunction with JavaScript can achieve dynamic content. JSON can be seen as a domain specific data source for browsers due to the nature of the relation between JSON and JavaScript.

John Leidegren
How is JSON more suited than XML to represent dynamic content ?
Guillaume
@John: You are mixing parsing with execution. They are two different beasts.
dirkgently
@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.
John Leidegren
+1  A: 

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, 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).

Richard Levasseur
+2  A: 

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.

VonC
+4  A: 

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 ...

Guillaume
+2  A: 

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").

StaxMan
+2  A: 

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.

KarstenF