Code Renaissance is about building great teams and great software. By exploring best practices, team interactions, design, testing and related skills Code Renaissance strives to help you create the team and codebase that you've always wanted.

The beauty of JSON with Ajax

JSON is a subset of the JavaScript that provides a fairly terse, dense data notation. When passed as data client-side it can be deserialized into a JavaScript Object with one line of code. In contrast XML is bulky and requires a fair bit of code client-side to parse and work with it. I believe that the denser notation and client side ease of use make JSON a ideal data transfer format for Ajax.

Lets take a quick look at some data in both XML and JSON formats...

XML:
<Table> <Rows> <Row><Item>1.1</Item><Item>1.2</Item></Row> <Row><Item>2.1</Item><Item>2.2</Item></Row> </Rows> </Table>

JSON:
{Rows:[{Item:[1.1,1.2]},{Item:[2.1,2.2]}]}

The leaner format can cut the size of your data in half and while you do loose a bit of the human readability with JSON, you can always use a tool like Charles to break it out into a tree view when debugging. The real beauty of JSON, however, is the ease of working with it on the client. If you deserialized the data into a variable called DataTable you can read first item in the second row by doing this:

DataTable .Rows[1].Item[0];

The trade off is that you will get cleaner, smaller client side code in exchange for more work getting your data into JSON on the server. Personally I'd rather have the extra code on the server where it can be unit tested and easily debugged than on the client where it can't.

Some might say that if you aren't using XML then you aren't doing Ajax. I disagree. I think that Ajax is actually a misnomer and so is XMLHttpRequest. XMLHttpRequest doesn't require that your data be in XML and will accept any format you choose. Believe it or not even JavaScript isn't even required to do Ajax; I hear VBScript works with XMLHttpRequest just fine.

I think that we've missed the big picture; JavaScript and XML aren't the reason why Ajax is such a phenomenal tool... it's the Asynchronous HTTP requests that it provides that make it so powerful. Also DOM scripting and DHTML are not Ajax, although they are used quite effectively in conjunction with it to provide the rich features that everyone associates with Ajax. The bottom line is that any site that makes Asynchronous HTTP requests is using Ajax and any site that doesn't, isn't.

The irony is that the one technology that is actually required for Ajax, HTTP, isn't even in the name. I think that AJAX would be better called Asynchronous HTTP-Request Scripting. Sadly AHS will never have the marketing appeal of the cool sounding Ajax, so we're stuck. As for the purists out there, I'm sure you'd prefer that I say that I am recommending "Asynchronous HTTP request scripting with JavaScript and JSON" than for me to talk about using JSON with Ajax. Sorry no such luck.

0 - What do you think?: