JSON
JSON (pronounced as
IPA , or like the English given name "Jason"), which stands for "
JavaScript Object Notation", is a lightweight
computer data interchange format.
JSON is a subset of the
object literal notation of JavaScript but its use does not require JavaScript.
The official MIME Media Type for JSON is
application/json.
JSON's simplicity has resulted in its widespread use, especially as an alternative to
XML in
Ajax. One of the advantages of
JSON over
XML as a data interchange format in this context is that it is much easier to write a JSON parser. In JavaScript, JSON can be parsed trivially using the
eval() procedure. This was important for the acceptance of JSON within the Ajax programming community because of JavaScript's ubiquity among
web browsers.
In practice, arguments regarding ease of parser development or parser performance are rarely relevant due to security concerns regarding the use of
eval() and the rise of built-in XML processing features in modern web browsers. For that reason JSON is typically used in environments where the size of the data stream between client and server is of paramount importance (hence its use by
Google,
Yahoo!, etc. which serve millions of users), where the source of the data can explicitly be trusted, and where the loss of fast access to client-side
XSLT processing for data manipulation or UI generation is not a consideration.
While JSON is often positioned "against" XML, it's not uncommon to see both JSON and XML used in the same application. For example, a client-side application which integrates
Google Maps data with
SOAP weather data requires support for both data formats.
There is growing support for JSON through the use of lightweight third-party packages. The list of supported languages includes
ActionScript,
C,
C#,
ColdFusion,
Common Lisp,
E,
Java,
JavaScript,
Lua,
ML,
Objective CAML,
Perl,
PHP,
Python,
Rebol, and
Ruby.
In December 2005, Yahoo! began supporting JSON as an option for some of its
Web services.
It is trivial to parse JSON in JavaScript using JavaScript's built in
eval() function. For example:
http_request.open("GET", url, true);
http_request.onreadystatechange = handle_json;
http_request.send(null);
function handle_json()
{
if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
var json_data = http_request.responseText;
var the_object = eval("(" + 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 although
syntactic variations are available on
Internet Explorer,
Opera,
Safari, and
Mozilla-based browsers.
Browsers can also use (hidden)
elements to asynchronously request JSON data in a
cross-browser fashion, or use simple
submissions. These approaches were prevalent prior to the advent of widespread support for XMLHTTPRequest.
The following is a simple example of a menubar definition using JSON and XML data encodings.
JSON:
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}One possible XML encoding:
JSON is usually less verbose than XML. Note that the two examples above are rather close in size when stripped of unnecessary whitespace. The use of
GZIP encoding to send data to the browser can also reduce any disparity between the formats regarding transmission size.
XML has wider support and offers many more development tools both client-side and server-side. Server-side JSON parsers are plentiful. For most environments, there is at least one JSON parser currently available. For some languages, such as Java and PHP, there are multiple implementation to choose from. In JavaScript, parsing is supported natively with the
eval() function. Both formats lack a rich mechanism for representing
large binary data types.
Regardless of how it compares to XML, when used effectively JSON can be very compact and efficient. As an example, the client
DHTML search application in
BarracudaDrive receives directory listings as JSON from the server. The search application constantly queries the server for new directories as it receives data from the server. The search application is remarkably fast, even over a slow link.
Server-side environments typically require the addition of a JSON-parsing object or function. Some programmers, especially those familiar with the
C family of languages, find JSON more natural than XML, but other developers can find its sparse notation confusing, particularly when dealing with deeply nested or strongly hierarchical data.
See more side-by-side comparisons of JSON and XML on this
JSON Example page.
Some of the limitations of JSON are addressed by
YAML. Although significantly more complex[
1], YAML is still considered lightweight. The
Ruby programming language uses YAML as its
de facto serialization format and can therefore handle JSON with particular ease.
*
YAML*
S-expressions
*
Ajax*
XMLThe Standard
*
The JSON Specification*
RFC 4627Tutorials
*
AJAX without XML Compares using XML, JavaScript Objects, and JSON
*
Speeding Up AJAX with JSON Demos how easier it is to reference JSON rather than XML.
*
JSON AJAX Chat Tutorial Simple tutorial that shows how to create a dynamic web chat
*
JSON/AJAX/PHP Simple tutorial and code libraries
Other
*
JSON Discussion Group*
JSON-RPC*
Relationship between JSON and YAML*
Ajax.NET Professional library to export .NET classes into JSON syntax and enable AJAX styled web applications + stand-alone JSON parser
*
Json.NET a .NET JSON API for reading, writing and serializing .NET objects to and from JSON
*
Jayrock implementation of JSON and JSON-RPC for the Microsoft .NET Framework.
*
SimpleJSON a simple and fast implementation of JSON for Python.
*
JSON: The Fat-Free Alternative to XML