Developer

JavaScript Object Notation: An alternative approach to data interchange

JavaScript Object Notation (JSON) is a very simple way to describe and transport text based on JavaScript's object notation. Find out why Tony Patton says JSON is a viable alternative to XML.

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 (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.

Weekly development tips in your inbox
Keep your developer skills sharp by signing up for TechRepublic's free Web Development Zone newsletter, delivered each Tuesday.
Automatically sign up today!

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:

{
"websites":[
"http://www.techrepublic.com/",
"http://www.cnet.com/",
"http://www.news.com/"
] }

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:

<?xml version="1.0"?>
<websites>
<website>http://www.techrepublic.com/</website>
<website>http://www.cnet.com/</website>
<website>http://www.news.com/</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:

{
"book":{
"title":"The Quiet American",
"author":"Graham Greene",
"date_published":"9/28/2004",
"publisher":"Penguin Classics",
"isbn": "0143039024",
"language":"English",
"format":"paperback",
"pages":"208"
} }

Here's the XML counterpart:

<books>
<book>
<title>The Quiet American</title>
<author>Graham Greene</author>
<date_published>9/8/2004</date_published>
<publisher>Penguin Classics</publisher>
<isbn>0143039024</isbn>
<language>English</language>
<format>paperback</format>
<pages>208</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:

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

Another approach is to use a JSON parser. The json.org Web site offers a parser that uses the eval procedure as well. In addition, the Ajax.NET Professional package includes a standalone parser for developers working with the Microsoft .NET Framework. The Yahoo! Developer Network provides libraries for parsing JSON with Python and PHP.

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.

Miss a column?

Check out the Web Development Zone archive, and catch up on the most recent editions of Tony Patton's column.

Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.

About Tony Patton

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

Editor's Picks

Free Newsletters, In your Inbox