December 20, 2014
Hot Topics:

Speeding Up AJAX with JSON

  • April 5, 2006
  • By Sean Kelly
  • Send Email »
  • More Articles »

Is JSON Fast and Reliable?

JSON produces slightly smaller documents, and JSON is certainly easier to use in JavaScript. XMLHttpRequest parses XML documents for you whereas you have to manually parse JSON, but is parsing JSON slower than parsing XML? I tested the XML parser built into XMLHttpRequest against JSON on these address cards and put them through thousands of iterations. Parsing JSON was 10 times faster than parsing XML! When it comes to making AJAX behave like a desktop application, speed is everything, and clearly, JSON is a winner.

Of course, you might not always have control of the server-side that's producing data for your AJAX application. You might be using a third-party server for your data and it's possible that server provides only XML output. And, if it happens to provide JSON, are you sure you really want to use it?

Notice in your example that you passed the response text directly into a call to eval. If you trust and control the server, that's probably okay. If not, a malicious server could have your browsers executing dangerous actions. In that case, you're better off using a JSON parser written in JavaScript. Luckily, one already exists.

Speaking of parsers, Python fans might've noticed that not only is JSON a subset of JavaScript, it's also a subset of Python. You can evaluate JSON directly in Python, or take advantage of a safe JSON parser instead. Parsers for JSON exist in dozens of other languages as well; the JSON.org Web site lists them.

JSON on the Server Side

So far, you've been focusing on using JSON in AJAX-based Web applications running in the client browser. Naturally, there has to be something in the Web server to produce that JSON in the first place. Luckily, creating JSON or "stringifying" it from existing data structures is fairly straightforward. Certain Web application frameworks, such as TurboGears, automatically include support for JSON output. JSON stringifiers exist for several languages as well.

In addition, commercial Web services providers are taking note of JSON. Yahoo recently made many of their Web services JSON-enabled. Yahoo's various search services, travel planners, del.icio.us, and highway traffic services all support JSON output. Doubtless, other major Web services providers will jump on the JSON bandwagon.

Conclusion

JSON's clever idea of being a subset of JavaScript (and Python) makes it an instantly useable, lightweight, and highly nimble way to handle data interchange for AJAX. It's faster to parse and vastly easier to use than XML. It's bound to become the buzzword of the day for "Web 2.0." Any developer, whether of the standard desktop application variety or of Web applications, is bound to appreciate its simplicity and adroitness. I hope you enjoy using JSON in your own buzzword-compliant, Web-2.0-based, AJAX-enabled, agile, rapid application development, on-or-off-rails applications.

About the Author

Sean Kelly is a fan of Greek mythology and comedy and appreciates the vast potential for puns in both AJAX and JSON. His own team of AJAX-led Argonauts are ready to tackle whatever tasks you might need to face in Web applications, Python, AJAX, Java, Web services, and even the most loathsome XML. He consults to the medical, aerospace, and digital media industries, but does yoga daily and is enormously flexible to whatever your application might be.

He resides in an undisclosed location with his wife Mary and daughter Ariana, who are routinely annoyed by his home automation hobby.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel