posts | commentsAdd to Google
18
Oct

JSON, the XML alternative

commentsNo comments

So whats up with the hype?

json JSON, the XML alternativeTo really appreciate JSON its good to understand XML and why an alternative to XML exists. XML is a powerful robust markup language that makes many things that were formerly very difficult much more feesable. One of those things is data interchange between different systems. Data interchange between different systems has always been possible, however up until recent years it has been very difficult because of the lack of a unified protocol. These days most modern languages and DBMSs have XML parsing functions that allow you to grab the data that you need and search and filter your data with Xpath. XML has many uses. Everything from styling to configuration files to web services and site maps. More recently, XML has been used to encapsulate data in AJAX HTTP requests. XML is very powerful in that it is platform independent so you can conceivably transfer data between multiple operating systems, languages, databases, and other systems and sub systems. You can see what XML looks like here

So why JSON?

While XML is extremely powerful, versatile and extensible, it also tends to be ‘heavy’ when transferring simple data structures over a tcp connection. JSON (Javascript Object Notation) tends to be much lighter weight than XML. For instance, a typical XML AJAX request looks something like the following (minus the HTTP headers):

<?xml version="1.0" encoding="UTF-8" 
  standalone="yes"?>
<request>
  <firstname>John</firstname>
  <lastname>Doe</lastname>
  <phone>555-444-6677</phone>
</request>

A JSON version of this would look something like the following:

'{"firstname" : "John", "lastname" : "Doe", "phone" : "555-444-6677"}';

As you can see this is significantly smaller than the XML request, and the more data sent the more significant this will be. Why do you care? All this info has to travel over the TCP connection to the web server and each character sent is an extra byte that will take up precious bandwidth.

Who uses JSON?

Most modern languages or frameworks have JSON functions that allow you to encode data (typically arrays of data) into a JSON string and send the data to the web server. PHP has JSON helper functions built in natively, and Javascript frameworks such as Prototype and JQuery have functions built in as well. This enables you to "package" up your data before sending it and minimize the extra "cruft" that is sent and received over your network. Other languages include C#, Java, Python, Perl, C++ and many more. Other technologies such as FireFox are using JSON as an XML alternative. For instance, Firefox is using JSON instead of XML to store bookmarks.

Conclusion

In the end, JSON can never really be what XML can potentially be. However, sometimes the lighter weight JSON can be a more efficient alternative to XML.

Bookmark and Share

Saturday, October 18th, 2008 at 6:52 am and is filed under JSON, PHP, Programming Methodologies. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply