Skip to content

Web2Coder

My Desktop Blog News Articles Tutorials Links Forum
You are here: Home arrow Blog arrow Introduction to JSON
Introduction to JSON Print E-mail
Written by Administrator   
Saturday, 28 April 2007
Tag it:
Stumble
Delicious
Digg
feedmelinks
Reddit
YahooMyWeb
Spurl
Technorati

Web services are practically synonymous with XML, the payload format used for communicating between client and server. However, the application architecture imposed by the appearance of Ajax and REST techniques has forced many to contemplate alternatives like JavaScript Object Notation (JSON).
JSON has come to the attention of Web service providers as a lighter and more friendly format for Web services clients in the form of a browser, or what would essentially be an Ajax-enabled application accessing RESTful Web services.

In this article we will cover an Introduction about JSON and its structure 

According to 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. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

The JavaScript Object Notation, or JSON, is a lightweight syntax for representing data. JSON's elegance comes from the fact that it's a subset of the JavaScript language itself.

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": [

"212 732-1234",

"646 123-4567"

]
}

The first thing you should realize about JSON is that it remains a simple text format—just like XML—which is relatively easy to read and inspect with the naked eye. At a syntax level, what starts to set JSON apart from other formats is the characters used to separate data, which are mainly constrained to apostrophes ', brackets ( ), [ ], { }, colons :, and commas ,. This listing illustrates what a JSON payload looks like:

{

"addressbook": {"name": "Mary Lebow",

"address": {

"street": "5 Main Street"

"city": "San Diego, CA",

"zip": 91912,

},

"phoneNumbers": [

"619 332-3452",

"664 223-4667"

]
}
}

To place this last JSON example in context, this listing shows the equivalent payload written in XML:

<addressbook>
<name>Mary Lebow</name>
<address>

<street>5 Main Street</street>

<city zip="91912"> San Diego, CA </city>

<phoneNumbers>

<phone>619 332-3452</phone>

<phone>664 223-4667</phone>

</phoneNumbers>
</address>
</addressbook>

As you can see, JSON has structure with nesting of data elements, just as XML does. JSON too is text-based, as XML is. Both use Unicode. JSON is just as readable by humans as XML is. Subjectively, JSON is clearer and has less redundancy. The JSON Web site describes the syntax of JSON rigorously, and yet does so briefly. It really is a simple little language! XML is certainly appropriate for marking up documents, but JSON is ideal for data interchange. Each instance of a JSON document describes one object with nested objects, arrays, strings, numbers, boolean values, or null values.

In these address card examples, the JSON version is lighter weight, taking 682 bytes of space, whereas the XML version requires 744. That's not a spectacular savings, of course. The real benefits come in the parsing.

 

Trackback(0)
Comments (1)Add Comment
thanks
written by majid, January 10, 2008
Thanks My Dear Freind smilies/wink.gif smilies/wink.gif

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley

security code
Write the displayed characters


Copyright 2007. All Rights Reserved.
busy
Last Updated ( Saturday, 28 April 2007 )
 
< Prev   Next >

Login


[+]
  • Narrow screen resolution
  • Wide screen resolution
  • Auto width resolution
  • Increase font size
  • Decrease font size
  • Default font size
  • fresh color
  • hot color
  • natural color
  • dark color