What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 6487 |  Welcome, Guest!   Register  Login
Home > Articles > JavaScript > JavaScript Object Notation (JSON)

JavaScript Object Notation (JSON)

Article posted by Lakhangarg on 9/1/2009 | Views: 4823 | Category: JavaScript | Level: Intermediate 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

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Lakhan Pal

Experience:4 year(s)
Home page:http://lakhangarg.blogspot.com
Member since:Monday, August 17, 2009
Level:Silver
Status: [Member] [Moderator]
Biography:Hello Friends
Myself Lakhan Pal Garg and i am a B.Tech (IT) Graduate and having 4 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.
>> Write Response - Respond to this post and get points
Related Posts

I am going to explain about javascript code for Drag from ASP.Net ListBox and to Drop in the specific Cursor location of TextArea .

To check the IE 8.0 Version after install in server

This article descibes how to write a javascript function that will check/uncheck all the checkboxes of the page in a single click.

I was wondering how to handle special chars in the query string when redirecting the page to a different page with the help of Javascript. I thought of sharing this snippet.

Many times we need a way to write object oriented code in javascript. We can create classes,objects,properties and lot more using javascript. This article include some of the features about object oriented scripting which i found very useful during developement.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 8:35:57 PM