Monday, January 4, 2016

JSON : JavaScript Object Notation

JSON is a syntax for storing and exchanging data.
JSON is a light weight data-interchange format.
JSON is language independent

A JavaScript program can use standard JavaScript function to convert JSON data into native JavaScript objects, instead of using any parser.

Difference between XML and JSON:

  • JSON doesn't use end tags
  • JSON is shorter
  • JSON is quicker to read and write
  • JSON can use arrays
  • XML has to be parsed with the XML Parser, JSON can be parsed by a standard JavaScript function.


Why JSON:

Using XML - 1. Fetch an XML document 2. Use XML DOM to loop through the document 3. Extract values and store in variables.

Using JSON - 1. Fetch a JSON string 2. JSON. Parse the JSON string

JSON is faster and easier than XML.

JSON Syntax:

The JSON syntax is a subset of the JavaScript syntax. JSON syntax rules:


  • Data is in name/value pair
              A name/value pair consists of a field name(in double quotes), followed by a colon, followed by a value. Example:"firstName":"John"
              JSON name requires double quotes. JavaScript names don't.
              JSON value can be an a number, a string, a Boolean, an array, an object, null.

  • Data is separated by commas
  • Curly braces holds objects
            JSON objects are written inside curly braces. JSON objects can contain multiple name/values pairs. Example : {"firstName":"John""lastName":"Doe"}
  • Square braces holds arrays.

          JSON arrays are written inside square braces. JSON array can contain multiple objects.
          Example:
          "employees":[
    {"firstName":"John""lastName":"Doe"}, 
    {"firstName":"Anna""lastName":"Smith"}, 
    {"firstName":"Peter","lastName":"Jones"}]










No comments:

Post a Comment