JSON syntax is regarded as a subset of JavaScript syntax. In this tutorial, you will learn about writing JSON Syntax.
JSON Syntax Rules
- Data is represented as name/value pairs and isolated by the comma.
- Curly braces organize objects, and the colon isolates each name.
- Square brackets hold the array, and commas separate values.
JSON Name / Value Pairs
A name/value pair is the name of a field, followed by a colon and value.
"userName" : "Alex"
It's easy to understand and equals to the Javascript statement:
userName = "Alex"
JSON Values
The following data types supported by JSON format:
Type | Description |
---|---|
String | Use in double quotes |
Number | Must be integer or floating point |
Object | An unordered collection of key: value pairs, used in curly brackets |
Array | An ordered sequence of values, used in square brackets |
Boolean | Must be true or false |
null | For empty value |
JSON Objects
Objects can contain multiple keys: value pairs:
{
"userName": "Alex",
"userAge": 26
}
It's also easy to understand and equals to the Javascript statement:
userName = "Alex"
userAge = 26
JSON Arrays
An array can contain multiple objects:
{
"students": [
{
"firstName": "Alex",
"lastName": "Rodriguez"
},
{
"firstName": "David",
"lastName": "Crosby"
},
{
"firstName": "Anna",
"lastName": "Weston"
}
]
}
In the above example, the object "students" is an array containing three objects, and each object is a record of students' names.