Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/articles/javascript-tutorials/)
-   -   JSON: JavaScript Object Notation (http://www.go4expert.com/articles/json-javascript-object-notation-t6312/)

pradeep 12Sep2007 13:41

JSON: JavaScript Object Notation
 
XML was developed and introduced as a vehicle for data interchange, but not all developers embraced it with open arms. Some developers actually opted to develop alternatives. One such alternative is JSON (http://www.json.org/) (JavaScript Object Notation). In this article, I take a closer look at the design and application of JSON.

What is it?



JSON is a simple way to format text using JavaScript's object notation. It is often described as data-oriented as opposed to the more popular object-oriented design of other technologies. Since it uses programming constructs, it is readable by both humans and machines.

JSON is designed to carry text-based data, so there is no support for working with any type of binary object. Basically, it supports two types of data structures: a collection of name/value pairs (like an object) and an ordered list of values (like an array). The following list takes a closer look at the data elements supported by JSON:

Objects: Objects begin and end with braces ({}).
Object members: Elements consist of strings and values separated by a colon (:). Multiple members are separated with a comma (,).
Arrays: Arrays are defined with braces; that is, they begin and end with braces ({}). Arrays contain values, which are placed between the braces with multiple values separated by commas.
Values: Individual values may be a string, numeric, object, or a literal value like true, false, and null. String values are enclosed in double quotes and contain Unicode characters and backslash or escape characters. A character is a single character string.

A key aspect of JSON is its simplicity -- it is much simpler than its XML counterpart. A good way to demonstrate this point is with an example that shows the syntax and formatting of JSON data. The following snippet uses JSON to create an array of Web site addresses:

Code: JavaScript

{
 "websites":[
 
 "http://www.go4expert.com/",
 
 "http://www.cfanatic.com/",
 
 "http://www.humourbox.info/"
 
 ] }

You may be scratching your head and thinking that XML could easily define the same data.

Why not XML?



XML is an excellent data vehicle. It allows you to create tags and so forth to describe any type and amount of data. In addition, it is a well known standard within the IT industry.

On the other hand, XML can be overkill for many tasks and can add a lot of unnecessary overhead. In addition, it is hard to read (by humans) regardless of how easy it is to create.

Let's format the previous example using XML:

Code: XML

<?xml version="1.0"?>
 <websites>
 <website>http://www.go4expert.com/</website>
 <website>http://www.cfanatic.com/</website>
 <website>http://www.humourbox.info/</website>
 </websites>

You may be thinking there is not much difference between the two examples; however, the number of characters that must be transmitted as the XML almost doubles the size (in characters). This may not be a big deal with a simple example, but let's consider a more complex example as the following JSON illustrates:

Code: JavaScript

{
 "book":{
 "title":"Five Point Someone",
 "author":"Chetan Bhagat",
 "date_published":"9/28/2004",
 "publisher":"Rupa Co.",
 "isbn": "978-8129104595",
 "language":"English",
 "format":"paperback",
 "pages":"270"
 } }

Here's the XML counterpart:

Code: XML

<books>
 <book>
     <title>Five Point Someone</title>
     <author>Chetan Bhagat</author>
     <date_published>9/8/2004</date_published>
     <publisher>Rupa Co.</publisher>
     <isbn>978-8129104595</isbn>
     <language>English</language>
     <format>paperback</format>
     <pages>270</pages>
     </book>
 </books>

The XML approach contains more text, resulting in more data transferred across the network. The amount of XML will grow as the data grows and/or becomes more complex.

Working with JSON data



In order to put JSON to use, you need to parse the JSON-formatted data just as you would its XML counterpart.

By way of its simple design, it is easy to parse JSON in JavaScript using JavaScript's built in eval() procedure as the following snippet demonstrates:

Code: JavaScript

testObject = eval('(' + json_data + ')');

Another approach is to use a JSON parser. The json.org Web site (http://www.json.org/json.js) offers a parser that uses the eval procedure as well. In addition, the Ajax.NET Professional package (http://www.codeplex.com/Release/Proj...ctName=AjaxPro) includes a standalone parser for developers working with the Microsoft .NET Framework. The Yahoo! Developer Network (http://developer.yahoo.com/) provides libraries for parsing JSON with Python (http://developer.yahoo.com/python/python-json.html), the CPAN module JSON (http://search.cpan.org/~makamaka/JSON-1.14/lib/JSON.pm) lets you parse JSON with Perl and PHP (http://developer.yahoo.com/php/howto-parseRestPhp.html).

Another Way To Go



JSON is a very simple way to describe and transport text based on JavaScript's object notation. JSON's best feature is speed because it allows you to transfer data using fewer characters than XML, thus making it move faster. While XML has its merits, I think JSON is a viable alternative to using XML for data interchange.


All times are GMT +5.5. The time now is 15:41.