JSON (JavaScript Object Notation)
Description
JSON is a lightweight text-based format for representing structured data. It is based on a subset of JavaScript, and historically one of its main attractions was the ability to be parsed using the JavaScript eval
function (although this is no longer the preferred method). There are six supported data types:
Type | Example |
---|---|
Object | { 'foo': 1, 'bar': 2 } |
Array | [ 1, 2, 3 ] |
String | "foo" |
Number | 2.71828 |
Boolean | true |
Null | null |
Objects and arrays may be nested, and may each contain a mixture of data types within them. Strings may contain any Unicode character, except that double quotes, backslashes and control characters must be escaped. JSON is usually encoded as UTF-8. Whitespace is permitted, but should typically be removed unless the JSON is intended to be human-readable.
Common uses of JSON include AJAX (as a replacement for XML), remote procedure calls using JSON-RPC, and for communicating with NoSQL databases.
Untrusted JSON expressions should not be parsed using eval
because this allows for arbitrary code execution. Instead, JavaScript 1.7 and above provide explicit support for JSON by means of the JSON.parse
and JSON.stringify
methods. Compatibility libraries such as JSON2 or JSON3 can be used to provide an implementation of this API in environments that do not already support it. Library support is similarly available in most other programming languages.
microHOWTOs
See also
Further reading
- JSON home page
- T.Bray (Ed), The JavaScript Object Notation (JSON) Data Interchange Format, RFC 7159, IETF, March 2014
- The JSON Data Interchange Format, ECMA-404, 1st Edition, October 2013
- JSON, Mozilla Developer Network