Wednesday, August 10, 2016

JSON tutorial

What is JSON?
JSON is text based, lightweight and language-independent data interchange format
JSON contains a small set of formatting rules for portable representation of structured data. 
  • JSON is known as JavaScript Object Notation.
  • The Douglas Crockford specified by format.
  • It is designed for human-readable data interchange
  • The filename extension is .json
  • JSON Internet Media type is application/json
  • JSON supports array, object, string, number and values.
 Uses of JSON 
  • Useful while writing JavaScript based application which has websites and browser extension.
  • JSON format is used for transmitting & serializing structured data over network connection.
  • Transmission of data between web application and server.
  • It is used with most of programming languages.
 Characteristics of JSON
·         JSON is easy to read and write.
·         It is a lightweight text-based interchange format.
·         JSON is language independent.

Simple Example in JSON
The following example shows how to use JSON to store information related to books based on their topic and edition.

{
   "book": [
           
      {
         "id":"01",
         "language": "Java",
         "edition": "third",
         "author": "Herbert Schildt"
      },
           
      {
         "id":"07",
         "language": "C++",
         "edition": "second",
         "author": "E.Balagurusamy"
      }
   ]
}

XML and JSON are designed for standardized and simple way of describing different kinds of hierarchical data structures and to facilitate their consumption and transportation in a standardized way.

Advantage of JSON Over XML: -
  • JSON is a valid subset of Python, JavaScript and YAML
  • JSON parsing is faster than XML parsing.
  • JSON is compact format.
  • Formatted JSON is easier to read than formatted XML.
  • JSON specifies how to represent complex datatypes.

Clearly, if the data utilized by web services grow, XML will loose an edge, Number of Bytes travelled between servers and clients is largely reduced if JSON is used for data exchange.

JSON vs XML
Below are the differences between XML and JSON.

1.  JSON format is lightweight over XML.
2. JSON is recognized natively by JavaScript.
3. JSON can contain integers, strings, lists, arrays. XML is just elements and nodes that need to be parsed into integers and so on before it can be consumed.
4. The most important disadvantage of JSON is that the format is very hard to read for humans, and that, of course, every single comma, quote, and bracket should be in exactly the correct place. While this is also true of XML, JSON’s welter of complicated-looking syntax, like the}}]} at the end of the data snippet, may frighten the newbies and make for complicated debugging.
5. Serialization format for your data, JSON is smaller, lighter weight and generally faster than XML.
6. JSON is best for consumption of data in web applications from web services for its size and ease of use, especially due to the built-in support in JavaScript.
Imagine the computation overhead for parsing an xml fragment compared to the instant lookup in JSON.
7. For configurations file XML is better choice to make because it more human readable.
8. A browser JSON is faster to serialize/deserialize as it’s simpler, more compact and more importantly natively supported.
9. XML is document-oriented. JSON is data-oriented. JSON can be mapped more easily to object-oriented systems.
10. XML and JSON both use Unicode. That help in support for internationalization.
11. JSON does not have a feature, so it is not well suited to act as a carrier of sounds or images or other large binary payloads. JSON is optimized for data.
12. XML documents can contain any imaginable data type – from classical data like text and numbers, or multimedia objects such as sounds, to active formats like Java applets or ActiveX components.
13. XML requires translating the structure of the data into a document structure. This mapping can be complicated. JSON structures are based on arrays and records. That is what data is made of. XML structures are based on elements (which can be nested), attributes (which cannot), raw content text, entities, DTDs, and other meta structures.

Below is the final summary.
1. For Data delivery between servers and browsers, JSON is better choice.
2. For storing Information in configuration files on the server side, XML is better choice.
3. On Browser Side: The speed and ease with which JSON is parsed and the ease of simple data retrieval from JavaScript object; makes JSON is a better choice.
4. Server Side: The querying data and format changes; makes XML a better choice.
Querying data: Using XPath, it’s possible to get direct access to a part of multiple parts of an XML data structure; no such interface exists for JSON. To get data from a JSON structure, you must know exactly where it is or else iterate over everything until you find it.
Format changes: You have your data in one format but you want it in another. If the data is in XML, you can write an XSLT template and run it over the XML to output the data into another format: HTML, SVG, plain text, comma-delimited, even JSON. When you have data in JSON, it’s pretty much stuck there. There’s no easy way to change it into another data format.
5. Security: JSON is less secure because of absence of JSON parser in the Browser; only way is to use eval() function. For security reasons on the browser side XML is better choice.
6. To extract data from database, XML is the only choice.


No comments:

Post a Comment