JSON stands for JavaScript Object Notation. It is a lightweight notation format that uses JavaScript syntax to provide a flexible, yet strict, method of storing and transmitting data. The language is flexible in that you can create and name the data structure in any way; it’s strict in that JSON follows a strict set of rules for its structure.
Following is an example of some basic JSON code:
{
"firstName": "Jason",
"lastName": "Jones",
"address": {
"city": "San Francisco, CA",
"zip": 94121,
"address": "444 Columbus Ave"
},
"email": [
"jason@sf.com",
"sjones@adobe.com"
]
}
The preceding code creates a JavaScript object that contains the data. A JavaScript object is a series of name and value pairs, written in a structured way. The basic structure of the object is as follows:
{name:value,name:value,name:value}
JSON supports a number of common data types. Each data type has a set of structural rules that you need to follow.
|
Data type |
Description |
Example |
|---|---|---|
|
Boolean |
In JavaScript, true and false are predefined keywords. To send a true value, send the word true. |
{"active":true} |
|
To send a false value, send the word false. |
{"active":false} |
|
|
String |
In JSON, you must delimit strings by using double quotation marks. For a list of what characters are allowed and what characters must be delimited, consult the JSON web site. |
{"address": "444 Columbus Ave"} |
|
Number |
Positive integers |
{"zip": 94121} |
|
Negative integers |
{"total points": -123} |
|
|
Floats |
{"length":122.2344} |
|
|
Scientific notation |
{"atoms per mole":-6.023e+23} |
|
|
Object |
Objects are delimited by braces ({ }), and contain zero (0) or more property and value pairs, separated by commas. The example shows an object with two properties on it: "count" and "results". The "count" property has an integer value of 2, and the "results" value is an array. |
{"count": 2, "results": [2, 3]} |
|
Array |
Arrays are delimited by square brackets ([ ]). Each item within the array is separated by a comma. Arrays can contain all supported JSON constructs. The example shows an array of five elements that includes Number, Boolean, String, Array, and Object data types. |
{"values":[1, false, "test", [2, 3], {"test": 1}]} |
|
Null |
In JavaScript, null is a predefined object. To send a null value in JSON notation, use the word null. The word null must be lower-case to be valid. |
{"email":null} |