Sunday, October 5, 2014

JSON


What is JSON?

JSON or JavaScript Object Notation, is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. JSON is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. 

Douglas Crockford was the first to specify and popularize the JSON format. JSON grew out of a perceived need for stateful, real-time server-to-browser communication without using browser plugins such as Flash or Java applets, which were the dominant method at the time.

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 can represent four primitive types (strings, numbers, Boolean and null) and two structured types (objects and arrays).

A string is a sequence of zero or more Unicode characters [UNICODE]. An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, Boolean, null, object, or array. An array is an ordered sequence of zero or more values. The terms "object" and "array" come from the conventions of JavaScript.

JSON's design goals were for it to be minimal, portable, textual, and a subset of JavaScript.

Advantages

JSON is easy for humans to read and write.
It is easy for machines to parse and generate.
JSON is a language-independent data format.
Compact data format to exchange data between applications.
JSON is very good tool support (almost every programming language supports JSON).

Less overhead while parsing and serialization than XML.
JSON can be parsed trivially using the eval() procedure in JavaScript

Applications

Web services responses. In the 'old' days, web services used XML as their primary data format for transmitting back data, but since JSON appeared, it has been the preferred format because it is much more lightweight.

If you are encoding PHP objects by default the encoding mechanism can only access public properties of these objects. When a method toJson() is implemented on an object to encode, Zend_Json calls this method and expects the object to return a JSON representation of its internal state.

Examples


Storing JSON Data

As a simple example, information about me might be written in JSON as follows:

var jason = {
"age" : "24",
"hometown" : "Missoula, MT",
"gender" : "male"
};

This creates an object that we access using the variable jason. By enclosing the variable’s value in curly braces, we’re indicating that the value is an object. Inside the object, we can declare any number of properties using a "name": "value" pairing, separated by commas. To access the information stored in jason, we can simply refer to the name of the property we need. For instance, to access information about me, we could use the following snippets:

document.write('Jason is ' jason.age);            // Output: Jason is 24

document.write('Jason is a ' jason.gender);   // Output: Jason is a male

Storing JSON Data in Arrays

A slightly more complicated example involves storing two people in one variable. To do this, we enclose multiple objects in square brackets, which signifies an array. For instance, if I needed to include information about myself and my brother in one variable, I might use the following:

var family = [{
    "name" : "Jason",
    "age" : "24",
    "gender" : "male"
},
{
    "name" : "Kyle",
    "age" : "21",
    "gender" : "male"
}];

To access this information, we need to access the array index of the person we wish to access. For example, we would use the following snippet to access info stored in family:

document.write(family[1].name);    // Output: Kyle
document.write(family[0].age);      // Output: 24


http://www.json.org/
http://www.secretgeek.net/json_3mins