JSON stands for JavaScript Object Notation.JSON is used for storing and exchanging data.It is important to understand that it is a subset of the javascript language.
It defines a set of rules for structuring the data.This means that it specifies how a certain piece of data should be structured.
JSON is a Data interchange Format
It is because of data interchange formats that applications are able to communicate with each other.Most of the applications needs to communicate with other applications.For example a news application may use a news web service to get the latest information.So the application may be using Java and may consume the web service developed in .NET.Data interchange
formats such as XML and JSON can be understood by both the applications and so are language independent.
XML vs JSON
The data between web server and web browser was usually exchanged using XML.JSON provides an alternative to that.
XML though works across different platforms but JSON is a better alternative because of the following reasons:
- JSON is easier to read and understand than XML.
- JSON is lightweight compared to XML.
- JSON is more suitable for the object oriented languages of today such as Java.
- Like XML it is text based and so works across different platforms.
It’s structure is simpler than XML so it is lightweight as compared to XML.For example to represent a contact entity in XML we will write XML as:
<contacts> <contact> <name>Tom</name> <phoneNo>111-12345</phoneNo> </contact> <contact> <name>Mark</name> <phoneNo>222-12345</phoneNo> </contact> <contacts>
We can represent the same entity in JSON as:
var contact=[{"name": "Tom","phoneNo":"111-12345"} {"name": "Mark","phoneNo":"222-12345"}]
Size of XML message is more than the same information represented in JSON.This difference can be huge if there are thousands of records.
This means that XML consumes more bandwidth than JSON.Today web applications are expected to have a very quick response time so JSON is the prefered format for web applications.
Data is represented in JSON using a collection of key/value pairs.Following is the syntax for structuring data in JSON
{ key1 : value1 , key2 :value2 …. keyN : value N}
- Following rules are used when structuring data
curly braces { and } are used for structuring data - key value pairs are declared within curly braces
- A key is of type string.value can be of different data types.
- As JSON is a subset of javascript so the values can be of the following data types.
Array
Boolean
Number
Object
String
Following represents a Student entity in JSON
var Student={"name": "John","class":"12"}