Monday, June 20, 2011

JSON vs XML


It depends on what type of client will be connecting to that web service.

If you plan to contact the web service from inside a browser a'la AJAX calls from Javascript, then JSON is the hands-down winner because it essentially is the serialization of Javascript objects. This property of JSON makes it a much better fit for client-side scripting in Javascript. JSON data can be regurgitated right into living, breathing Javascript objects without much hullabaloo.

XML and JSON were designed for two distinct purposes. XML originated as a way to give semantic definition to text in documents. JSON on the other hand is specifically for serializing data structures.

Both can do what each other can do, but those fundamentally different base design rationales are I think evident in how you work with each type of markup.

True, XML can represent data structures, but an example of describing structures like empty arrays in XML shows off how JSON is much better at describing data structures.

JSON on the other hand is woefully mismatched to describe semantic meaning behind text in documents, which is where XML excels (think HTML).

No easy answer. The requirements of your connection client and the type of data you need to serialize should drive the decision between XML and JSON.

JSON for web services answering in-browser callbacks, and XML for, well, XML for when, well.

1 comment:

  1. Hi Balaji!!!

    Great article!!!

    As I am new o mobile development, I would like to know when choose Json instead XML as webservice data format for an native Android app. I realised Json is fashion buzzword, but I cant realized when choose one or another. Could you help me?

    Regards,

    Iury

    ReplyDelete