JSON, 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 ubiquitousness among web browsers.) JSON is 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.
Comparison to XML
Proponents of JSON claim that its simpler structure and less verbose formatting make it easier and faster to parse than XML. Claims have also been made that JSON is easier to read for many programmers due to its similarity to the C family of languages.
On the other hand, it could be argued that JSON lacks XML's wide industry support, extensibility, and display capabilities. It is also missing a good mechanism for representing large binary data types. (It can be argued that XML is also lacking a good mechanism.)
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); var json_data = http_request.responseText; eval("var the_object = ("+json_data+")");
Please note that the JavaScript code above did not use XMLHttpRequest in a cross-browser fashion. Also, please note that the example above used XMLHttpRequest in a synchronous fashion, to make the code easier to understand. In general though, one should be using XMLHttpRequest in an asynchronous fashion.
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 pronunciation
JSON is pronounced like the name "Jason" -- jā'sən.