JavaScript JSON Syntax
JSON has a very simple syntax for representing structured data.
JSON has a very simple syntax for representing structured data.
One JSON document consists of one JSON value.
The value can be:
- An Object
- An Array
- A String
- A Number
- A Boolean
- or null
JSON Object Literals
This is a JSON string:
Example
Example Code
javascript'{"name":"John", "age":30, "car":null}'Inside the JSON string there is a JSON object literal:
Example
Example Code
javascript{"name":"John", "age":30, "car":null}A JSON object is written between curly braces { }.
An object contains zero or more properties.
Properties comes in key/value pairs separated by commas.
key names are strings enclosed in double quotes.
Keys and values are separated by a colon.
Values must be a valid JSON data type. JSON supports six value types:
| Value | Example |
|---|---|
| String | "John" |
| Number | 42 |
| Boolean | true |
| Null | null |
| Object | {"name" : "John"} |
| Array | [1, 2, 3] |
Share
JSON Array Literals
This is a JSON string:
Example
Example Code
javascript'["Ford", "BMW", "Fiat"]'Inside the JSON string there is a JSON array literal:
Example
Example Code
javascript["Ford", "BMW", "Fiat"]Other Examples
Example Code
javascript[
"Apple",
"Banana",
"Orange"
]A JSON array is written between square brackets [ ].
An array contains zero or more values.
Values are separated by commas.
Values can be any of the six JSON data types:
| Value | Example |
|---|---|
| String | "John" |
| Number | 42 |
| Boolean | true |
| Null | null |
| Object | {"name" : "John"} |
| Array | [1, 2, 3] |
JSON Does Not Allow
JSON has a small, strict syntax.
The following JavaScript features are not valid JSON:
| Feature | JSON | JS |
|---|---|---|
| Unquoted property names | No | Yes |
| Single quoted strings | No | Yes |
| Trailing commas | No | Yes |
| Comments | No | Yes |
JSON Property Names
Every property name must be a string.
Property names must be enclosed in double quotes.
Valid:
Example Code
javascript{
"name": "John"
}JSON Strings
JSON strings must use double quotes.
Valid:
Example Code
javascript{
"city": "London"
}Whitespace
Whitespace is ignored outside strings.
You can use spaces, tabs, and line breaks to make JSON easier to read.
Example
Example Code
javascript{"name":"John", "age":30}Trailing Commas
JSON does not allow a comma after the last property or array value.
Wrong:
Example Code
javascript{
"name": "John",
"age": 30,
}Comments Are Not Allowed
JSON does not support comments.
Wrong
Example Code
javascript{
// Customer name
"name": "John"
}Interactive Code Examples
Example 1
javascript'{"name":"John", "age":30, "car":null}'Example 2
javascript{"name":"John", "age":30, "car":null}Example 3
javascript'["Ford", "BMW", "Fiat"]'Example 4
javascript["Ford", "BMW", "Fiat"]Other Examples
javascript[
"Apple",
"Banana",
"Orange"
]