JavaScript Object Notation: An alternative approach to data interchange
Takeaway: 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.
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.
Print/View all Posts Comments on this article
|
|
|
|
White Papers, Webcasts, and Downloads
- The Impact of Virtualization Software on Operating Environments VMware Today's use of virtualization technology allows IT professionals to ... Download Now
- The Scalable Enterprise: VMware ESX Server on the Dell PowerEdge 6650 Dell This paper introduces the server virtualization software, VMware ESX ... Download Now
- VMware Infrastructure: A Guide to Bottom-Line Benefits VMware Frustrated by the high cost of maintaining or building ever-larger data centers? Get the facts you need to formulate your Virtualization Action Plan. Download Now
- Five Steps to Determine When to Virtualize YourServers VMware Thinking of virtualizing the servers at your company? Use this step-by-step guide to determine when's the best time to make your big move. Download Now
- Building the Virtualized Enterprise with VMware Infrastructure VMware This paper explains how adopting a virtual infrastructure -- comprised of server, storage, and networking virtualization technologies -- can help your organization build a sustainable competitive ... Download Now
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET


