JSON: JavaScript Object Notation

Discussion in 'JavaScript and AJAX' started by pradeep, Sep 12, 2007.

  1. pradeep

    pradeep Team Leader

    Apr 4, 2005
    Likes Received:
    Trophy Points:
    Kolkata, India
    Home Page:
    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:

     ] }
    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"?>
    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:

     "title":"Five Point Someone",
     "author":"Chetan Bhagat",
     "publisher":"Rupa Co.",
     "isbn": "978-8129104595",
     } }
    Here's the XML counterpart:

         <title>Five Point Someone</title>
         <author>Chetan Bhagat</author>
         <publisher>Rupa Co.</publisher>
    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 (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/ProjectReleases.aspx?ProjectName=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.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice