Introduction of JSON

JSON ( Java Script Object Notation) is a lightweight data-interchange format compared to XML.
 
JSON format is easy to read and write for developers and supports text format.
  1. A JSON object starts with { (left brace) and ends with (Left braces ) }
     
  2. .name/value is defined by : (colon)
     
    Like { “Name”:” Neeraj”}
     
  3. .name/value pairs separated by , (comma)
     
    {“first”:” Neeraj”,” second”:” Kumar”}
     
  4. JSON data Structure:
     
    Object:
     
    {“key1”:”value1”,”key2”:”value2”}
     
    Ex:
     
    {“first”:” Neeraj”,” second”:” Kumar”}
     
    Array:
     
    [“first”,” second”,” third”]
     
    Number:
     
    12 23
     
    String:
     
    “this is a text”
     
    Boolean:
     
    True False
     
  5. Personal information about a Person in JOSN:
    1. {  
    2.    “name”:” Neeraj Kumar”,  
    3.    “birthday”:” November 5 1994”,  
    4.    “address”: “Behind atta Mandir, Alwar,Rajasthan, INDIA”  
    5. }  
    Another way of representing the same information in JSON:
    1. {“first_name”: ”Neeraj”,  
    2.     “last_name”: ”Kumar”,  
    3.     “birthday”: ”11 - 5 - 1994”“address”: {“street”: ”Behind Atta Mandir”,  
    4.         “City”: ”Alwar”,  
    5.         “State”: ”Rajasthan”,  
    6.         “Country”: ”INDIA”  
    7.     }  
    8. }  
    Both are the same but the second example clears all information about the person and it is a formal way of representing the data of a person.
     
  6. JSON Schema according to Second Example:
    1. {  
    2.     "type""object",  
    3.     "properties": {  
    4.         "first_name": {  
    5.             "type""string"  
    6.         },  
    7.         "last_name": {  
    8.             "type""string"  
    9.         },  
    10.         "birthday": {  
    11.             "type""string",  
    12.             "format""date-time"  
    13.         },  
    14.         "address": {  
    15.             "type""object",  
    16.             "properties": {  
    17.                 "street": {  
    18.                     "type""string"  
    19.                 },  
    20.                 "city": {  
    21.                     "type""string"  
    22.                 },  
    23.                 "State": {  
    24.                     "type""string"  
    25.                 },  
    26.                 "Country": {  
    27.                     "type""string"  
    28.                 }  
    29.             }  
    30.         }  
    31.     }  
    32. }  
  7. JSON Object in javascript:
    1. Var jsObject=JSON.parse(data);  
    Ex:
    1. <html>  
    2.     <title>JSON object in javascript</title>  
    3.     <body>  
    4.         <h2 id="jsonobject"></h2>  
    5.         <script>  
    6.             var myProfile = '{"first_name":"Neeraj Kumar","last_name":"Kumar","address":{"street":"Behind atta mandir","city":"Alwar","State":"Rajasthan","Country":"INDIA"}}';  
    7.             var jsObject = JSON.parse(myProfile);  
    8.             document.getElementById("jsonobject ").innerHTML =  
    9.             jsObject.first_name + "  
    10.             <br>" +  
    11.             jsObject.last_name + "  
    12.                 <br>" +  
    13.             jsObject.address.street + "  
    14.                     <br>" +  
    15.             jsObject.address.city + "  
    16.                         <br>" +  
    17.             jsObject.address.State + "  
    18.                             <br>" +  
    19.             jsObject.address.Country ;  
    20.   
    21.         </script>  
    22.     </body>  
    23. </html>  
Member can be access using . (dot).