Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

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:

Difference between JSON and XML


JSONXML
 Stands ForJSON : “JavaScript Object Notation”.XML: “Extensible Markup Language”.
 Extended FromJSON is extended from JavaScript.XML is extended from SGML : “Standard Generalized Markup Language“.
 Purpose JSON is one type of text-based format or standard for interchanging  data i.e.human readable.  JSON is developed by “Douglas Crockford”. XML is a Markup Language having format that contains set of rules for the encoding the documents which is readable for bothhuman & machine. XML is developed by W3C :“World Wide Web Consortium “.
 Syntax JSON syntax is lighter than XML as JSON has serialized format of data having  less redundancy. JSON does not contain start and end tags. XML is not so lighter as JSON as having start and end tags and it takes more character than JSON to represent same data.
SpeedJSON is light – weighted in compare to XML as it has serialized format and so faster also.XML is not so light weighted as JSON.
Support of Data Type & ArrayJSON supports datatype including integer and strings, JSON also supports array.XML does not provide any data type so needs to be parsed into particular datatype. No direct support for array also.
Object SupportJSON has support of native objects.XML can get support of objects through mixed use of attributes & elements.
CommentsJSON does not support CommentsXML supports comments.
NamespaceJSON does not have support for Namespaces.XML supports Namespaces.
MappingJSON is data oriented and can be mapped more easily.XML is document oriented and needs more effort formapping.
ParsingJSON uses only evel() for parsing i.e. for interpreting the JavaScript code & returns the result. It does not need any additional code for parsing.XML needs XML Document Object Model (DOM) implementation & with that additional code for mapping text back to the JavaScript objects.
ApplicationFor Web services, JSON is better.For configuration, XML is better.
Changing FormatYou can not change JSON data to otherformat.In XML, using XSLT you can change XML data into another format like comma –delimited, plain text, JSON, etc.
AccessIn JSON no such is interface for getting direct access to a part in JSON data-structure.In XML, Using XPath, it is possible to get the direct access to a particular part of multiple parts of XML data- structure.