JavaScript JSON Syntax
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
- null
JSON Example
A JSON object contains one or more name/value pairs.
Example
{
"name": "John",
"age": 30,
"city": "New York"
}
Note
Property names must be enclosed in double quotes.
JSON Objects
A JSON object begins with { and ends with }.
An object contains zero or more name/value pairs.
Name/value pairs are separated by commas.
Example
{
"firstName": "John",
"lastName": "Doe",
"age": 30
}
JSON Arrays
A JSON array begins with [ and ends with ].
An array contains zero or more values.
Values are separated by commas.
Example
[
"Apple",
"Banana",
"Orange"
]
JSON Values
JSON supports six value types.
| Value | Example |
|---|---|
| String | "John" |
| Number | 42 |
| Boolean | true |
| Null | null |
| Object | {"name":"John"} |
| Array | ["A","B"] |
JSON Property Names
Every property name must be a string.
Property names must be enclosed in double quotes.
Valid JSON
{
"name": "John"
}
Invalid JSON
{
name: "John"
}
JSON Strings
JSON strings must use double quotes.
Valid JSON
{
"city": "London"
}
Invalid JSON
{
"city": 'London'
}
Whitespace
Whitespace is ignored outside strings.
You can use spaces, tabs, and line breaks to make JSON easier to read.
Example
{"name":"John","age":30}
Equivalent JSON
{
"name": "John",
"age": 30
}
JSON Does Not Allow
JSON has a small, strict syntax.
The following JavaScript features are not valid JSON:
| Feature | Allowed |
|---|---|
| Comments | No |
| Single quoted strings | No |
| Unquoted property names | No |
| Trailing commas | No |
| Functions | No |
undefined |
No |
JSON and JavaScript
JSON syntax is similar to JavaScript object syntax.
However, JSON is a text format with stricter rules.
| JavaScript Object | JSON |
|---|---|
| Property names may be unquoted | Property names must use double quotes |
| Strings may use single or double quotes | Strings must use double quotes |
| Comments are allowed | Comments are not allowed |
| Functions are allowed | Functions are not allowed |
undefined is allowed |
undefined is not allowed |
| Trailing commas are allowed in modern JavaScript | Trailing commas are not allowed |
Common JSON Mistakes
JSON has stricter syntax rules than JavaScript objects.
Property Names Must Use Double Quotes
Wrong
{
name: "John"
}
Correct
{
"name": "John"
}
Strings Must Use Double Quotes
JSON strings cannot use single quotes.
Wrong
{
"name": 'John'
}
Correct
{
"name": "John"
}
Trailing Commas Are Not Allowed
JSON does not allow a comma after the last property or array value.
Wrong
{
"name": "John",
"age": 30,
}
Correct
{
"name": "John",
"age": 30
}
Comments Are Not Allowed
JSON does not support comments.
Wrong
{
// Customer name
"name": "John"
}
undefined Is Not a JSON Value
JSON supports null, but not undefined.
Wrong
{
"city": undefined
}
Correct
{
"city": null
}
Functions Are Not JSON Values
Wrong
{
"greet": function() {
return "Hello";
}
}
JSON can contain strings, numbers, objects, arrays, Booleans, and null.