I personally really like JSON, but there is still some debate around the whole XML vs JSON thing. So, in the spirit of true “10 reasons to/for/why <something>”, I present to you 5 reasons Why and 5 reasons Why Not to use JSON in your web development or AJAX project!
Why
- JSON is Javascript Dont waste time parsing the XML: eval() lets you deserialise JSON data into objects with minimal effort or development time.
- JSON is lightweight JSON can house the same data as XML, but wastes less data on packaging, for example the XML declaration is wasted bandwidth to your users.
- JSON is simple Unlike XML, there are no corporations struggling for “control” of JSON by piling on standard upon standard. Kiss your namespace, schema, dtd, standards-compliance worries good bye.
- JSON is object-oriented XML is document-orientated, meaning you often carry a lot of extra cruft just to send a tiny bit of data back to the client.
- JSON has wide-spread support There are lightweight libraries designed to work with JSON in many languages, including C/C++/C#, Java, PHP, ASP, Perl, Python, Ruby and more!
Why Not
- XML has near universal support JSON is gaining ground, but XML is still number one when it comes to support from both programming languages and development tools themselves.
- XML lets you package anything Using CDATA, you can send pretty much anything with XML; JSON only allows for text-data.
- XML’s Schemas/DTD gives you control You can enforce compliance for data entering your software by validating it against a schema first.
- XML is extensible JSON is great for just serialising and deserialising objects, but XML allows greater flexibility and scope for other uses, for example XSLT.
- XML isn’t Javascript If the idea of having your code just happily eval() the data it gets sent freaks you out as a security issue, then parsing a XML document is for you!
Basically, both JSON and XML are platform and programming language agnostic with wide spread support. Both have their pros and cons; personally I think that JSON is better suited for AJAX style software where you are deserialising on the client, with XML is better suited for the more hands-on data shunting at the back end.