Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? Is there a difference in performance? i.e. are browsers able to process one format faster than the other?

like image 898
flybywire Avatar asked Feb 25 '09 06:02

flybywire


People also ask

When working with Ajax applications which is faster XML or JSON?

JSON is faster because it is designed specifically for data interchange. JSON encoding is terse, which requires less bytes for transit. JSON parsers are less complex, which requires less processing time and memory overhead. XML is slower, because it is designed for a lot more than just data interchange.

What are the advantages and limitations of JSON over XML?

In most scenarios, JSON is undoubtedly easier to read in its expanded form than XML. JSON can have a substantially lower character count reducing the overhead in data transfers. JSON is much easier to parse. But this is only relevant if one is writing a parser which is not a common activity at this point.

Why Is JSON better suited for Ajax?

This makes the application faster and more responsive to user actions. Although X in Ajax stands for XML, JSON is preferred because it is lighter in size and is written in JavaScript. Both JSON and XML are used for packaging information in the Ajax model.


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

like image 58
John Leidegren Avatar answered Sep 23 '22 11:09

John Leidegren