JSON Syntax
Storing Data
When storing data, the data has to be a certain format, and regardless of where you choose to store it, text is always one of the legal formats.
JSON makes it possible to store JavaScript objects as text.
JSON Syntax Rules
The JSON syntax is a subset of the JavaScript syntax.
JSON syntax is derived from JavaScript object notation syntax:
- Data is in name/value pairs,
- Data is separated by commas,
- Curly braces hold objects, and
- Square brackets hold arrays.
{
"employees": [
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"Anna", "lastName":"Smith" },
{ "firstName":"Peter", "lastName":"Jones" }
]
}
|
JSON Data—a Name and a Value
JSON data is written as name/value pairs (aka key/value pairs).
A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value:
JSON—Evaluates to JavaScript Objects
The JSON format is almost identical to JavaScript objects.
In JSON, keys must be strings, written with double quotes:
In JavaScript, keys can be strings, numbers, or identifier names:
{ name: "John" } // JavaScript
|
Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.