JSON or JavaScript Object Notation is popular lightweight data exchange/ interchange and storage format. JSON syntex is both human and machine readable. JSON was build in 2001 but JavaScript starts supporting JSON in ECMA5. File extension for JSON file is .json. As compare to XML, JSON is easy to parse.

Difference between JSON and XML


JSON in JavaScript

JSON Object was introduced in JavaScript in ECMA5 (2009). In Javascript, JSON is string representation of JavaScript Object. The key and value both must be quoted in JSON. But functions can't be used in JSON to create methods.

JavaScript JSON Example

    var user='{"name":"john","age":"22"}';                            

JSON Object

ECMA5 introduced the global JSON Object in javascript. The JSON Object can have two methods, parse and stringify to convert json to object and object to string.

JSON Methods

JSON Parse

JSON Parse method is used to convert JSON String to JavaScript Object. The JSON Object is used to convert string to object, followed by parse method.

    var user1='{"name":"john","age":"22"}'; 
    var user2=JSON.parse(user1);
    typeof user1                 // returns string            
    user2 instanceOf(Object)     // returns string         

JSON Stringify

JSON Stringify method is used to convert JS Object to JSON String. The JSON Object is followed by stringify method.

    var user1={"name":"john","age":"22"}; 
    var user2=JSON.stringify(user1);