JavaScript Object Notation (JSON)

Lakhangarg
Posted by in JavaScript category on for Intermediate level | Views : 6671 red flag

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. JSON is a language independent text format which is fast and easy to understand. JavaScript Object Notation (JSON) is a text format for the serialization of structured data. File extension for json file is *.json

The application/json is media type for JavaScript Object Notation (JSON).
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. JSON is a language independent text format which is fast and easy to understand. JavaScript Object Notation (JSON) is a text format for the serialization of structured data. File extension for json file is *.json

The application/json is media type for JavaScript Object Notation (JSON). JSON can represent four primitive types:
•    Strings (double-quoted Unicode with backslash escaping)
•    Numbers (integer, real, or floating point)
•    Booleans (true and false)
•    Null
And two structured types:
•    Objects (collection of key:value pairs, comma-separated and enclosed in curly braces)
•    Arrays (an ordered sequence of values, comma-separated and enclosed in square brackets)

A JSON text is a serialized object or array.

      JSON-text = object / array

   These are the six structural characters:

      begin-array    [ left square bracket

      begin-object   { left curly bracket

      end-array      ] right square bracket

      end-object     } right curly bracket

      name-separator  : colon

      value-separator , comma

The following example shows the JSON representation of an object that describes a person. The object has string fields for first name and last name, contains an object representing the person's address, and contains a list of phone numbers (an array)

.{
     "firstName": "John",
     "lastName": "Smith",
     "address": {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumbers": [
         { "type": "home", "number": "212 555-1234" },
         { "type": "fax", "number": "646 555-4567" }
     ]
 }

The equivalent for the above in XML:
<Person firstName="John" lastName="Smith">
  <Address>
    <streetAddress>21 2nd Street</streetAddress>
    <city>New York</city>
    <state>NY</state>
    <postalCode>10021</postalCode>
  </Address>
  <phoneNumber type="home">
      212 555-1234
  </phoneNumber>
  <phoneNumber type="fax">
      646 555-4567
  </phoneNumber>
</Person>

JSON is like XML because:
   1. They are both 'self-describing' meaning that values are named, and thus 'human readable'
   2. Both are hierarchical. (i.e. You can have values within values.)
   3. Both can be parsed and used by lots of programming languages
   4. Both can be passed around using AJAX (i.e. httpWebRequest)

JSON is Unlike XML because:
   1. XML uses angle brackets, with a tag name at the start and end of an element: JSON uses squiggly brackets with the name only at the beginning of the element.
   2. JSON is less verbose so it's definitely quicker for humans to write, and probably quicker for us to read.
   3. JSON can be parsed trivially using the eval() procedure in JavaScript
   4. JSON includes arrays {where each element doesn't have a name of its own}
   5. In XML you can use any name you want for an element, in JSON you can't use reserved words from JavaScript

What is good about JSON?
When you're writing Ajax stuff, if you use JSON, then you avoid hand-writing xml. This is quicker.
Again, when you're writing Ajax stuff, which looks easier? The XML approach or the JSON approach:
The XML approach:
   1. Bring back an XML document
   2. Loop through it, extracting values from it
   3. Do something with those values, etc,
The JSON approach:
   1. Bring back a JSON string.
   2. 'eval' the JSON

Page copy protected against web site content infringement by Copyscape

About the Author

Lakhangarg
Full Name: Lakhan Pal
Member Level: Silver
Member Status: Member,Moderator
Member Since: 8/17/2009 12:39:46 AM
Country: India

http://lakhangarg.blogspot.com
Hello Friends Myself Lakhan Pal Garg and i am a B.Tech (IT) Graduate and having 8+Years of Exp. in Microsoft Technology. I have Write a Blog Named Free Code Snippets (http://lakhangarg.blogspot.com/) I hope you must visit my blog as your valuable feedback will motivate me to write more and improve my mistake. If you want to gain more knowledge then share it with others.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)