Data Types, Syntax and Example
JSON's basic types are:
- Number (double precision floating-point format in JavaScript, generally depends on implementation)
- String (double-quoted Unicode, with backslash escaping)
- Boolean (
true
orfalse
) - Array (an ordered sequence of values, comma-separated and enclosed in square brackets; the values do not need to be of the same type)
- Object (an unordered collection of key:value pairs with the ':' character separating the key and the value, comma-separated and enclosed in curly braces; the keys must be strings and should be distinct from each other)
null
(empty)
Non-significant white space may be added freely around the "structural characters" (i.e. brackets "{}", colons ":" and commas ",").
The following example shows the JSON representation of an object that describes a person. The object has string fields for first name and last name, a number field for age, contains an object representing the person's address, and contains a list (an array) of phone number objects.
{ "firstName": "John", "lastName": "Smith", "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 10021 }, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] }One potential pitfall of the free-form nature of JSON comes from the ability to write numbers as either numbers or strings. Consider for example a value such as the zip code 10021. 10021 may be written with quotes by one writer but without by another. This could give surprises or even errors when e.g. postal codes are exchanged between systems or even searched for within the same system. Searching for the a double quoted "10021" won't necessarily find an unquoted 10021. In addition, postal codes in the USA are numbers but other countries use letters as well. This is a type of problem that the use of a JSON Schema (see below) is intended to reduce.
Since JSON is a subset of JavaScript, it is possible (but not recommended, because it allows the execution of arbitrary JavaScript wrapped in function blocks) to parse JSON text into an object by invoking JavaScript's eval
function. For example, if the above JSON data is contained within a JavaScript string variable contact
, one could use it to create the JavaScript object p
as follows:
The contact
variable must be wrapped in parentheses to avoid an ambiguity in JavaScript's syntax.
The recommended way, however, is to use a JSON parser. Unless a client absolutely trusts the source of the text, or must parse and accept text that is not strictly JSON-compliant, one should avoid eval
. A correctly implemented JSON parser will accept only valid JSON, preventing potentially malicious code from being executed inadvertently.
Browsers, such as Firefox 4 and Internet Explorer 8, include special features for parsing JSON. As native browser support is more efficient and secure than eval
, native JSON support is included in the recently-released Edition 5 of the ECMAScript standard.
Read more about this topic: JSON
Famous quotes containing the word data:
“This city is neither a jungle nor the moon.... In long shot: a cosmic smudge, a conglomerate of bleeding energies. Close up, it is a fairly legible printed circuit, a transistorized labyrinth of beastly tracks, a data bank for asthmatic voice-prints.”
—Susan Sontag (b. 1933)