Monday, December 30, 2013

A short guide to JSON - JavaScript Object Notation

JavaScript Object Notation

  • Lightweight, open standard, human-readable, language/platform independent and self-describing text-based designed for storing and transferring structured data.
  • This format was specified by Douglas Crockford. <LINK>
  • JSON Internet Media type is application/json.

Use Cases:

  • Serializing and transmitting structured data over network connection.
  • Web Services and API's use JSON format to provide public data.
  • Fetch JSON data from a web server, convert it to a JavaScript object, use it to manipulate the UI.

Advantages:

  • Easy for computers and humans to read and write.
  • Supports tree like Hierarchies in data.
  • Can be easily mapped to data structures used by most programming languages (numbers, strings, booleans, nulls, arrays and associative arrays).
  • Most programming languages support JSON.

Limitations:

  • Capable of representing numbers, booleans, strings, null, and arrays and objects. Doesn't natively support complex data types like functions, regular expressions, dates etc.
  • Doesn't have a widely accepted schema for defining and validating the structure of JSON data.

Syntax:

  • JSON defines only two data structures: objects and arrays. An object is a set of name-value pairs, and an array is a list of values.
  • JSON defines six data types: string, number, object, array, true, false and null.
  • Objects are enclosed in braces ({}).
  • Name and value in a pair are separated by a colon (:). 
  • Name-Value pairs are separated by a comma (,).
  • Names in an object are strings, whereas values may be of any of the six data types, including another object or an array.
  • Arrays are enclosed in brackets ([]), and their values are separated by a comma (,).
  • Each value in an array may be of a different type, including another array or an object.

JSON Example:

var emptyObj = {};

var empObj = {eid: '7918', ename: 'Prince', grade: 'T3'};
 
{
"empObjs": [
                   { "eid":"7918" , "ename":"pkapoor", "grade":"T2" },
                   { "eid":"709" , "ename":"jai", "grade":"T4" },
                   { "eid":"13386" , "ename":"rahul", "grade":"T3" }
                   ]
}

Deserialize JSON 

  • Javascript eval() and JSON Parsor:
    <script>
    
    var txt = '{"empObjs":[' +
        '{"eid":"7918","ename":"pkapoor" },' +
        '{"eid":"709","ename":"jai" },' +
        '{"eid":"13386","ename":"rahul" }]}';

        var obj = JSON.parse(txt);
        // var obj = eval ("(" + txt + ")");
        document.getElementById("eid").innerHTML=obj.employees[1].eid         document.getElementById("ename").innerHTML= obj.employees[1].ename     </script>

Serialize JSON 

  • JSON Stringify
    • JSON.stringify() outputs JSON strings with all whitespace removed. It makes it more compact for sending around the web.
        /* Put this line in above example and print the value. */
        var text = JSON.stringify(obj);

JSON vs XML

  • JSON is smaller than XML, faster, easier to parse and more human readable format.
  • XML is more verbose than JSON, so it's faster to write JSON for humans.
  • JavaScript's eval method parses JSON. When applied to JSON, eval returns the described object.
  • The Ajax (Asynchronous JavaScript And XML) originally used XML to transmit data between server and browser, but now JSON has become a more popular way to carry Ajax data.
  • XML is a tried-and-tested technology and is used in a huge range of applications.
  • Doesn't have a widely accepted schema for defining and validating the structure of JSON data.

JSON and Java

Java APIs for JSON processing are easily available and easy to use..
javax.json package - contains reader, writer, model builder interfaces for the object model along with other utility classes and Java types for JSON elements.
javax.json.stream package - contains a parser interface and a generator interface for the streaming model.
Refer to http://docs.oracle.com/javaee/7/tutorial/doc/jsonp001.htm for more details.

JSON and AJAX

The Ajax (Asynchronous JavaScript And XML) originally used XML to transmit data between server and browser, but now JSON has become a more popular way to carry Ajax data.

For Further Reading:

JSONPath
JXON
JSON-LD

Reference

http://docs.oracle.com/javaee/7/tutorial/doc/jsonp001.htm#BABEECIB
http://www.json.org/js.html
http://www.youtube.com/watch?v=wbB3lVyUvAM

No comments:

Post a Comment