5 April 2014

JSON vs XML


Similarities between JSON and XML:

·         JSON is plain text like XML

·         JSON is “self-describing”(human readable) and “self-understandable” as XML

·         JSON is hierarchical (values within values)

·         JSON can be parsed by JavaScript

·         JSON data can be transported using AJAX

 

Differences between JSON and XML:

·         No end tag like XML

·         Shorter than XML

·         Quicker to read and write when compare to XML

·         Can be parsed using built-in JavaScript functions like eval() and parse()

·         Uses arrays – Efficient for handling huge data

·         No reserved words

 

Why JSON?

JSON is faster and easier than XML when you are using it in AJAX web applications:

Steps involved in exchanging data from web server to browser involves:

Using XML

·         Fetch an XML document from web server

·         Use the XML DOM to loop through the document

·         Extract values and store in variables

·         It also involves type conversions

Using JSON

Fetch a JSON string
Parse the JSON string using eval() or parse() javascript functions

 

Definition

XML and JSON are both designed to form a simple and standartized way of describing different kinds ofhierarchical data structures and to facilitate their transportation and consumption in a standartized way. Nonetheless, there are few substantial differences that deserve attention. 

The major differences

Take a look at the following example:
JSON :

XML :
Clearly XML is somehow easier to understand for a human. That’s why it’s commonly used for configuration files (although lambdas and fluent APIs are recently emerging as a way of configuration). JSON, on ther other hand, is harder to read, but only from a human perspective. ​​
Aside from that, XML is also a markup language (as the name suggests) and is therefore suitable for document description – the hierarchical elements can also have attributes, which is not present in JSON. On ther other hand, JSON has a very concise syntax for defining lists of elements, which makes it preferrable for text format object serialization.
XML is also the usual choice of base when it comes to Domain Specific Languages

JSON and JavaScript

JSON's succinct syntax makes it very light and compact. It is also easier to parse from JavaScript, and that’s one of the main reasons it’s preferred for browser-based client-side applications (note that JSON stands for JavaScript Object Notation, it’s just natural). If you want to work with XML from JavaScript, you'll need to use an extra library for that. With JSON, you can just parse the message (let’s say with JSON.Parse() ) and work directly with js objects.
For example:

No comments:

Post a Comment