What are the advantages and disadvantages of json vs xml for ajax requests? [closed] What are the advantages and disadvantages of json vs xml for ajax requests? [closed] ajax ajax

What are the advantages and disadvantages of json vs xml for ajax requests? [closed]


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


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.


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.