Sunday, August 3, 2008

Why JSON over XML in AJAX?

Many web applications nowadays use AJAX to help them in decreasing server load and of course speed up the website. AJAX have been part of the web 2.0 technologies and is used widely in many websites.

AJAX have this idea of calling back the server from the client side to accomplish some task, such as posting back data to the server or getting new information from the server. it can been seen as form submitter, tips of the day ticker, tabbed boxes and more.

However, developers have the choice of language of the message that is responded from the server after the postback or request have been made by AJAX from client side. It can either be in the form of Javascript Object Notation (JSON) or Extensible Markup Language (XML).

JSON generally have advantages over XML - firstly because JSON is much lightweight compared to XML. As JSON only contains one character notations, it saves more bandwidth and load time compared to XML which uses tags.

Secondly, the multiple-dimension JSON is also attractive to developers, as JSON's parsing on PHP is easy, with only 2 functions, whereas XML uses multiple functions to parse - which may be confusing.

Thirdly, JSON is directly readable on client side using Javascript, thus you save time parsing it on server AND client side. For XML, you will need to parse the data again in order to use it.

To use JSON directly in Javascript: var myObject = eval('(' + myJSONtext + ')');

For course, the X in AJAX originally refers to XML - asynchronous JavaScript and XML - but as time goes by, developers will find satisfying format for their needs of communication between server and client. Even in ASP.net, AJAX messages in Updatepanels and other controls are split using "|".

No comments: