Andy Grove's Blog

Is JSON just for AJAX?

Wednesday, 11 July 2007

I picked up a free copy of Head Rush Ajax at JavaOne this year and just picked it up again as I've been meaning to find out more about Ajax for some time.

I've used roughly similar principles in the past, long before the term Ajax was invented. Back then, around 1998, it was often referred to as DHTML. As part of a project I architected for BSkyB at that time, I built a web interface onto a TV sheduling application that used Javascript to send simple GET requests to a servlet which then returned data in a proprietary text format and the Javascript parsed this text and called document.write() to render the data within the web page.

The most interesting part of the book for me was the discussion around using JSON rather than XML to receive data from the server. JSON (JavaScript Object Notation) is the native text representation of JavaScript objects and is arguably more suitable than XML for return data to an Ajax client for two reasons:

  1. JavaScript can convert the JSON text to a real JavaScript object using the standard eval() function, whereas XML data needs to be accessed via the DOM, which requires more code.
  2. JSON provides a more concise data format, therefore reducing network traffic.
JSON is obviously a much simpler format than XML by design and can not support the same rich data structures that are possible with XML. At the same time, for certain applications it may well provide sufficient functionality at a far lower cost in terms of parsing performance and document size.

One observation I had while reading the book is that Ajax doesn't seem like a great acronym really, since without XML it would really just be "Asynchronous JavaScript". Mind you, having a catchy name for this approach has certainly helped it gain popularity.

Labels: ,