HTML-Advisor
Mostly practical advices

AJAX and JSON Part 2

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.

Source: www.developer.com

Tags: , , , , , , , ,

Related:

Posted in AJAX, JSON, JavaScript. 1,729 views
Responses are currently closed, but you can trackback from your own site.

1 Comment

Sorry, the comment form is closed at this time.