2007/11/05

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data format based on the object notation of the JavaScript language. It is minimal, textual, and a subset of JavaScript. Specifically, it is a subset of ECMA-262 (The ECMAScript programming Language Standard, Third Edition, December 1999).


JSON has been widely adopted by people who found that it made it a lot easier to produce distributed applications and services. It is lightweight and very easy to parse by any language and libraries and tools exist in many languages to handle JSON. There are now JSON libraries or built-in JSON support for these programming languages and systems: ActionScript, C, C++, C#, Cold Fusion, D, Delphi, E, Erlang, Haskell, Java, Lisp, LotusScript, Lua, Perl, Objective-C, OCAML, PHP, Python, Rebol, Ruby, Scheme, and Squeak.

JSON has been formalized in RFC 4627. The MIME Media Type is application/json. JSON has no version number. No revisions to the JSON grammar are anticipated. If something has a 1.0, it will inevitably get a 1.1 and a 2.0, and everything is crap until it is 3.0. JSON is very stable.

The character encoding of JSON text is always Unicode. UTF-8 is the only encoding that makes sense on the wire, but UTF-16 and UTF-32 are also permitted. A JSON decoder MUST accept all well-formed JSON text. A JSON decoder MAY also accept non-JSON text. A JSON encoder MUST only produce well-formed JSON text. This is consistent with Postel's Law: "Be liberal in what you accept, and conservative in what you send."

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.


JSON Format Overview

JSON is a very simple text format based on JavaScript's object notation. The notation contains these basic elements:

  • Objects. Objects begin and end with curly braces ({}).
  • Object members. Members consist of strings and values, separated by colon (:). Members are separated by commas.
  • Arrays. Arrays begin and end with braces and contain values. Values are separated by commas.
  • Values. A value can be a string, a number, an object, an array, or the literals true, false, or null.
  • Strings. Strings are surrounded by double quotes and contain Unicode characters or common backslash escapes.

A simple example of JSON output looks like this:

{
"Image": {
"Width":640,
"Height":480,
"Title":"Screen shot of my web",
"Thumbnail":
{
"Url":"http:\/\/myweb.com\/image\/837492",
"Height": 160,
"Width": "120"
},
"IDs":[ 123, 467, 87346 ]
}
}

In this example, Image is a top-level object; all other data is a member of that object. Width, Height and Title are all simple members containing number and string data. Thumbnail is a nested object, containing members for URL, Height and Width. IDs is an array, containing number values. Note the string value for URL, in which the slashes are escaped ("\/").



For a complete description of JSON and its many uses, we suggest a visit to Douglas Crockford's JSON.org, with a side trip to JSON: The Fat-Free Alternative to XML, on the same server.

No comments:

Monetize Your Web!