JSON

From Wikipedia, the free encyclopedia.

Jump to: navigation, search

JSON (pronounced like the name "Jason" -- jā'sən), which stands for "JavaScript Object Notation", is a lightweight computer data interchange format. JSON has the advantage, over XML, as a data interchange format in that it can be trivially parsed, by JavaScript, with JavaScript's built in eval() procedure. (This is important because of JavaScript's ubiquity among web browsers.) JSON is a subset of JavaScript's object literal format.

The name can be misleading, as JSON is a language-independent text format. There is growing support for JSON in other languages through the use of lightweight 3rd-party packages. The list of supported languages includes ActionScript, C, C#, ColdFusion, E, Java, JavaScript, ML, Objective CAML, Perl, PHP, Python, Rebol, and Ruby.


Contents

Using JSON

JSON is useful as a data-interchange format in Ajax applications because it can be trivially parsed by JavaScript's built in eval() procedure. For example:

myObject = eval("return " + json_data);

Typically, one would get the JSON data from a web server using the XMLHttpRequest object. For example:

 var http_request = new XMLHttpRequest();
 var url = "http://example.net/this/is/a/fake/url/"; // This URL should give us JSON data. 
 
 // Download the JSON data from the server.
 http_request.onreadystatechange = handle_json;
 http_request.open("GET", url, false);
 http_request.send(null);
 
 function handle_json() {
        if (http_request.readyState == 4) {
                if (http_request.status == 200) {
                        var json_data = http_request.responseText; 
                        eval("var the_object = ("+json_data+")");
               } else {
                       alert('There was a problem with the URL.');
               }
               http_request = null;
       }
}

Note that the use of XMLHttpRequest in this example is not cross-browser. Also note that this example uses XMLHttpRequest synchronously so that the code is easier to read; in general, however, XMLHttpRequest should be used asynchronously.

Browsers can also use <iframe> to asynchronously request JSON data in a cross-browser fashion.

There is a Ajax.NET Library for the Microsoft .NET Framework that will export .NET classes into JSON syntax to communicate between the client and the server, both directions.

JSON example

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

The same text expressed as XML:

<menu id="file" value="File">
  <popup>
    <menuitem value="New" onclick="CreateNewDoc()" />
    <menuitem value="Open" onclick="OpenDoc()" />
    <menuitem value="Close" onclick="CloseDoc()" />
  </popup>
</menu>

Comparison to XML and other markup languages

In a JavaScript environment, JSON is easier and faster to parse than XML. Server-side environments require the addition of a JSON-parsing object or function. Some programmers, familiar with the C family of languages, find JSON more natural than XML but other developers can find its sparse notation confusing.

XML has wider industry support and offers far more server-side development tools. Server-side JSON parsers are scarce by comparison. For any single environment, there are only one or two JSON parsers currently available. On the other hand, client-side parsing is supported natively with JavaScript's eval() function. Both formats lack a rich mechanism for representing large binary data types.

See more side-by-side comparisons of JSON and XML on this JSON Example page.

YAML, the data serialization language favored by the Ruby programming language is a superset of JSON with a handful of extended data types. JSON is much easier to parse, though[1]. This coincidence that allows you to share lightweight representations of data on the server-side and client side makes for quite elegant integrations between Rails & javascript.

Other simplified markup languages

External links

Personal tools
In other languages