Dictionary of Meaning
<<Back
Please select a letter:
A |
B |
C |
D |
E |
F |
G |
H |
I |
J |
K |
L |
M |
N |
O |
P |
Q |
R |
S |
T |
U |
V |
W |
X |
Y |
Z |
0-9
Click here for Shopping
JSON
*** Shopping-Tip: JSON
'''JSON''' (pronounced as
International Phonetic Alphabet IPA {{IPA|[dʒeɪsən]}}, or like the English given name "Jason"), which stands for "
JavaScript '''J'''ava'''S'''cript Object (computer science) '''O'''bject '''N'''otation", is a lightweight
computer data interchange format. '''JSON''' is a subset of the
object literal notation of JavaScript but its use does not require XML.
JSON's simplicity has resulted in its widespread use, especially as an alternative to
XML in
Ajax (programming) Ajax. One of the claimed 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 itself, 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 programming language C,
C Sharp C#,
ColdFusion,
E programming language E,
Java programming language Java,
JavaScript,
ML programming language ML,
Ocaml Objective CAML,
Perl,
PHP,
Python programming language Python,
Rebol,
Ruby programming language Ruby, and
Lua programming language Lua.
In December 2005
Yahoo! began supporting JSON as an option for some of its [http://developer.yahoo.net/common/json.html Web services].
JSON does not provide support for object references.
Using JSON
In theory, it is trivial to parse JSON in JavaScript using JavaScript's built in
eval() procedure. For example:
myObject = eval('(' + json_data + ')');
In practice, however, security and other considerations generally preclude using ''eval'' on raw data and a separate JavaScript parser would have to be used to ensure security. The [http://www.JSON.org/json.js open-source parser] provided by JSON.org uses
eval() in its parse function, guarding it with a regular expression so that the eval function only sees safe expressions.
One JSON data access example via
XMLHTTP XMLHTTPRequest is:
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, true);
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
syntax syntactic variations are available on
Internet Explorer,
Opera (web browser) Opera,
Safari (web browser) 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.
There is a [http://ajax.schwarz-interactive.de 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
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()"}
]
}
}}
XML:
While JSON proponents often point to JSON as being less verbose than XML, note that the two examples above are 189 characters and 190 characters respectively 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. In fact, when GZIP is used on the previous examples the XML version is actually 6 bytes smaller than the JSON data. While this is hardly conclusive, it shows that experimentation with the specific data set is required to determine which format will be more efficient in terms of size. It's not an absolute that JSON is smaller than XML. JSON's benefit is not that it is smaller on the wire, but that it better represents of the structure of the data, and so requires less coding and processing.
Comparison to XML and other markup languages
XML has wider support and offers many more development tools both client-side and server-side. Server-side JSON parsers are scarce. 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 (assuming the source is trusted, as discussed above). Both formats lack a rich mechanism for representing
Binary large object large binary data types.
When dealing with large data sets XML data processed via XSLT can be particularly efficient, eclipsing the performance of equivalent JSON-to-markup processing. For example, processing a set of XML-based records into an HTML list can be significantly faster than processing the same records from JSON to HTML. While not all major browsers currently support client-side XSLT this is an important consideration for users of Internet Explorer, Opera, and Mozilla-based browsers.
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 [http://barracudaserver.com/examples/BarracudaDrive/CommandLine.html#API 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
:Category:C programming language family 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 [http://www.JSON.org/example.html JSON Example page].
Some of the limitations of JSON are addressed by
YAML, which is a
superset of JSON. Although significantly more complex[http://bob.pythonmac.org/archives/2005/07/19/what-happened-to-yaml/], 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.
Other simplified markup languages
*
YAML, which happens to be a superset of JSON
*
S-expressions
See also
*
Ajax (programming) Ajax
External links
-
JSON Website
-
JSON Discussion Group
-
JSON-RPC
-
JSON: The Fat-Free Alternative to XML
-
relationship between JSON and YAML
-
AJAX without XML, but with JSON
-
JSON Tutorial
Category:JavaScript programming language
Category:Markup languages
de:JSON
pl:JSON
fr:JSON
*** Shopping-Tip: JSON