JSON

JSON or JavaScript Object Notation is popular text format. JSON is lightweight data and can be used to exchange, interchange and storage data. JSON syntax 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 vs XML
JSON Vs XML

JSON Example


[
    {"name":"abc","age":21,"isAlive":true},
    {"name":"pqr","age":22,"isAlive":true},
    {"name":"xyz","age":20,"isAlive":true}
]

JSON Applications

  1. Rest API
  2. No SQL


JSON in JavaScript

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

Supported datatypes in JSON

  1. String
  2. Numbers
  3. Boolean
  4. Null
  5. Array
  6. Objects

Not allowed in JSON Strings

  1. undefined
  2. function

JSON Object with strings

<script>
    var user='{"name":"john","age":"22"}';                                                       
</script>                              

JSON Object with numbers

<script>
    var user='{"name":"john","age":22}';                                                       
</script>                              

JSON Object with boolean

<script>
    var user='{"name":"john","age":22, "isAlive":true}';                                                       
</script>                              

JSON Object with null

<script>
    var user='{"name":"john","age":22, "id":null}';                                                       
</script>                              

JSON Object with Object

<script>
    var user='{"name":"john","dad":{"name":"Mac","age":55}}';                                                       
</script>                              

JSON Object with Array

<script>
    var user='{"name":"john","hobbies":["cricket","football"]}';                                                       
</script>                              

JSON Object

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


    var j=JSON;
    console.log(j);        // return object
    

JSON Methods

Do remember to parse correct JSOn String to avoid parsing error.


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.

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

JSON Stringify

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

<script>
    var user1={"name":"john","age":"22"};     // object
    var user2=JSON.stringify(user1);          // string
                               
</script>