Sunday, September 2, 2012

JSON-Java Script Object Notation

Introduction
JSON is lightweight data interchange format. It is easy to understand and describe because it is a plain text. Since it is text format, hence language independent. It is much like XML but easier and faster. It uses conventions like C, C++, C #, Java, JavaScript and many other languages.

JSON is syntax for storing and exchanging text information like as XML.It is a subset of JavaScript. It uses JavaScript syntax for describing data objects, but is still language and platform independent. It is like XML because it is plain text and human readable, hierarchical and can be parsed by JavaScript.

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 built on two structures:
1. A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
2. An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

JSON Syntax Rules

1. Data should be in pairs separated by colon (":")
For example, “Name":"Ravi”. This is similar to JavaScript syntax: Name="Ravi"
2. Data should be separated by comma (,)
For example, “Name":"Ravi”, “Phone":"9656757865","Age":"23"
3. Objects should be in curly brackets ("{}").
{"Name":"Ravi”, “Phone":"9656757865","Age":"23"}
4. array should be defined in square bracket("[]").
"information":{[{"Name":"Ravi”, “Phone":"9656757865","Age":"23"},
                       {"Name":"Javi","Phone":"7756757865","Age":"24"}
]}

JSON Values
JSON values can be a number or a string or a Boolean or an array or an object or null.

JSON Object
For example,
{"Name":"Ravi”, “Phone":"9656757865","Age":"23"}

JSON Array
{"information":{[{"Name":"Ravi","Phone":"9656757865","Age":"23"},

{"Name":"Javi","Phone":"7756757865","Age":"24"}
]}
};

Creating objects in JSON
Since JSON uses JavaScript syntax, will see how to create object in JSON
var object1={"information":[
{"Name":"Ravi”, “Phone":"9656757865","Age":"23"},
{"Name":"Javi","Phone":"7756757865","Age":"24"}
]
};
Here object1 is a object and information is a single member of it. Member bindings containing two objects each containing three members Name, Phone and Age.

 How to retrieve member of object?
In the given example, for retrieving member we can write

object1.information [0].Phone //we will get "9656757865"
object1.information [1].Age //we will get "24"

How to convert JSON text into a object?
Since JSON uses JavaScript syntax, method eval() is used for this. So to avoid ambiguity we prefer the text to be wrapped in parenthesis.
For example, var object2=eval ('(''+object1+)');
The eval function is very fast.However,it can compile and execute any JavaScript program, so there can be security issues. The use of eval is preferred when the source is trusted and competent. It is much safer to use JSON parser.

JSON Parser
In web applications over XMLHttpRequest, communication is permitted only to the same origin that provided the page, so it is trusted but it cannot be competent. If server is not rigorous in its JSON parser encoding or if it does not scrupulously validate all of its inputs, then it could deliver invalid JSON text that could be carrying dangerous script. The eval function would execute the script, unleasing its malice.

To defend against this, a JSON parser should be used. A JSON parser will recognize only JSON text, rejecting all scripts. In browser that provides native JSON support, SON parsers are much faster than eval. It is expected that native JSON support will be included in the next ECMAScript standard.
For example:
data1=JSON.parse (Object1, reviver);
Here reviver is a function that will be called for every key and value at every level of this final result. Each value will be replaced by the result of reviver function. This can be used to reform the generic objects into instances of pseudo classes or transform date string into date objects.

JSON Stringifier
It converts JavaScript data structure into JSON text.


No comments:

Copyright © Codingnodes,2014.All Rights Reserved.