Tuesday, 15 May 2012

Alternative of XML: JSON (JavaScript Object Notation)

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.JSON is a text format that is completely language independent.

The JSON format is often used for serializing and transmitting structured data over a network connection. It is used primarily to transmit data between a server and web application, serving as an alternative to XML. JSON is a subset of The file type for JSON files is ".json". The MIME type for JSON text is "application/json"

Data Types in JSON

Data structures in JSON are based on key / value pairs. The key is a string, the value can be a numerical value, a boolean value (true or false), array or an object.

1. Number (integer or floating point)
2. String  (in double quotes)
3. Boolean (true or false)
4. Array (an ordered sequence of values, comma-separated and enclosed in square brackets; the values do not need to be of the same type)
Example: var continents = ["Europe", "Asia", "Australia", "Antarctica", "North  America", "South America", "Africa"];
5. Object (an unordered collection of key:value pairs with the ':' character separating the key and the value, omma-separated and enclosed in curly braces; the keys must be strings and should be distinct from each other)
Example: var mailingAddress = {  "Address" : "123 Anywhere St.",  "City" : "Springfield", "PostalCode" :   99999 };
6. null (empty)

JSON Parser

Since JSON syntax is a subset of JavaScript syntax, the JavaScript function eval() can be used to convert a JSON text into a JavaScript object. The eval() function uses the JavaScript compiler which will parse the JSON text and produce a JavaScript object.

The eval() function can compile and execute any JavaScript. This represents a potential security problem. It is safer to use a JSON parser to convert a JSON text to a JavaScript object. A JSON parser will recognize only JSON text and will not compile scripts. In browsers that provide native JSON support, JSON parsers are also faster. Native JSON support is included in newer browsers and in the newest ECMAScript (JavaScript) standard.

Similarities between XML and JSON

1. JSON and XML are plain text
2. JJSON and XML are "self-describing" (human readable)
3. JSON and XML are hierarchical.
4. JSON and XML can be parsed by JavaScript
5. JSON and XML data can be transported using AJAX

Differences between XML and JSON

1. Data types

XML: Does not provide any notion of data types. One must rely on XML Schema for adding type information.
JSON: Provides scalar data types and the ability to express structured data through arrays and objects.

2. Support for arrays

XML: Arrays have to be expressed by conventions, for example through the use of an outer placeholder element that models the arrays contents as inner elements. Typically, the outer element uses the plural form of the name used for inner elements.
JSON: Native array support

3. Support for objects

XML: Objects have to be expressed by conventions, often through a mixed use of attributes and elements.
JSON: Native object support.

4. Null support

XML: Requires use of xsi:nil on elements in an XML instance document plus an import of the corresponding namespace.
JSON: Natively recognizes the null value

5. Comments

XML: Native support and usually available through APIs.
JSON: Not supported.

6. Namespaces

XML: Supports namespaces, which eliminates the risk of name collisions when combining documents. Namespaces also allow existing XML-based standards to be safely extended.
JSON: No concept of namespaces. Naming collisions are usually avoided by nesting objects or using a prefix in an object member name.

7. Formatting decisions

XML: Complex. Requires a greater effort to decide how to map application types to XML elements and attributes. Can create heated debates whether an element-centric or attribute-centric approach is better.
JSON: Simple. Provides a much more direct mapping for application data. The only exception may be the absence of date/time literal.

8. Size

XML: Documents tend to be lengthy in size, especially when an element-centric approach to formatting is used.
JSON: Syntax is very terse and yields formatted text where most of the space is consumed (rightly so) by the represented data.

9. Parsing in JavaScript

XML: Requires an XML DOM implementation and additional application code to map text back into JavaScript objects.
JSON: No additional application code required to parse text; can use JavaScript's eval function.

10. Learning curve

XML: Generally tends to require use of several technologies in concert: XPath, XML Schema, XSLT, XML Namespaces, the DOM, and so on.
JSON: Very simple technology stack that is already familiar to developers with a background in JavaScript or other dynamic programming languages.

11. Tools

XML: Enjoys a mature set of tools widely available from many industry vendors.
JSON: Rich tool support—such as editors and formatters—is scarce.

12. Reserved Keywords

XML: There are reserved keywords.
JSON: No reserved keywords

JSON: An Alternative of XML

1. When data is encoded in XML, the result is typically larger than an equivalent encoding in JSON, mainly because of XML's closing tags. JSON is smaller than XML, and faster and easier to parse.

2. AJAX Support in JSON: Althought most browsers can construct, send, and parse XML, JavaScript Object Notation (or JSON) provides a standardized data exchange format that is better-suited for Ajax-style web applications.

For AJAX applications, JSON is faster and easier than XML:

Using XML

1. Fetch an XML document
2. Use the XML DOM to loop through the document
3. Extract values and store in variables

Using JSON

1. Fetch a JSON string
2. eval() the JSON string

No comments:

Post a Comment