What is JSON?

JSON and XML both are human readable formats and are language independent. They both have support for creation, reading and decoding in real world situations. In this article i have described disadvantages of XML, overview of JSON and advantages of JSON over XML.

XML is mainly use to transfer data from between multiple technologies. So if we already have XML for transferring data then why JSON is required?

What are the problems or disadvantages of XML?

There are two main disadvantages of XML:

  • Difficulties in XML Parsing

    For some light weight technologies like JavaScript XML parsing is not very easy. Once it gets the XML string emitted from other technologies it get the values (data) inside it with the help of custom logic.

  • Heavy weight

    We can easily see many unnecessary tags in complex data of XML which increase the weight of XML.


What is JSON?

JSON stand for JavaScript Object Notation which is a lightweight text-based, human-readable data-interchange format that is completely language independent but with some conventions.

The two primary parts that make up JSON are keys and values. Together they make a key/value pair.

  • Key: A key is always a string enclosed in quotation marks.
  • Value: A value can be a string, number, boolean expression, array, or object.
  • Key/Value Pair: A key value pair follows a specific syntax, with the key followed by a colon followed by the value. Key/value pairs are comma separated.

Syntax

"key" : value
"FirstName" : "Manish"

This example is a key/value pair. The key is "title" and the value is "Blog".

JSON Object

JSON objects are written inside curly braces.

var customer = '{"FirstName": "Manish", "LastName": "Banga", "Address": "India"}';

JSON Arrays

JSON arrays are written inside square brackets.

var customers = '[
        {"FirstName": "Manish", "LastName": "Banga", "Address": "India"},
        {"FirstName": "Debashish", "LastName": "Saha", "Address": "India"},
        {"FirstName": "Abhinav", "LastName": "Dwivedi", "Address": "India"}
     ]';

Is it same as JavaScript object?

Not, it just a format in which we can represent data whereas JavaScript object is a variable which actually holds our data. There is no direct relation between JavaScript object and JSON. JSON is called as JavaScript object notation because JSONS structure looks like JavaScript object.

Advantages of JSON

  • JSON is very simple, shorter while in XML we have to explicitly specified end tags of all elements which make it heavy weight. XML is more verbose than JSON, so it's faster to write JSON for humans.
  • It is platform independent just like XML. Almost all modern technologies have the ability to read and understand it.
  • XML has to be parsed with an XML parser, JSON can be parsed by a standard JavaScript function.