JSON has a simple set of rules for writing data. Every JSON document must follow these rules so that computers can read and understand it correctly.
A key-value pair is the smallest piece of information in JSON.
The key tells you what the information is.
The value is the actual information.
For example:
{
"color": "Blue"
}
Here:
"color" is the key."Blue" is the value.You can think of a key as a label and the value as the information attached to that label.
An object is a group of related key-value pairs.
Use an object when you want to describe one thing using different pieces of information.
For example, if you want to store information about one student:
{
"name": "Liam",
"grade": 8,
"passed": true
}
This object stores several details about one student.
An array is a list of values.
Use an array when you need to store more than one value for the same piece of information.
For example, if a student likes several sports:
{
"favoriteSports": [
"Football",
"Swimming",
"Cricket"
]
}
Here, favoriteSports has three values, so an array is used.
If there is only one sport, you don't need an array:
{
"favoriteSport": "Football"
}
An array is useful whenever you have a list of items, such as:
| Rule | Description |
|---|---|
| Data is in name/value pairs | A key followed by a colon and a value: "key": value |
| Keys must be strings | Keys must be wrapped in double quotes: "name" (not name) |
| Values have types | A value can be a string, number, boolean, null, object, or array |
| Commas separate pairs | Each key/value pair is followed by a comma, except the last |
| Curly braces for objects | { } wrap an object containing key/value pairs |
| Square brackets for arrays | [ ] wrap an ordered list of values |
Every JSON document starts and ends with either { } (an object) or [ ] (an array).
{
"name": "Alice",
"age": 30,
"city": "New York"
}
| Invalid | Why | Fix |
|---|---|---|
{ name: "Alice" } | Key missing double quotes | { "name": "Alice" } |
{ 'name': 'Alice' } | Single quotes not allowed | { "name": "Alice" } |
{ "name": "Alice", } | Trailing comma after last pair | { "name": "Alice" } |
{ "age": 01 } | Leading zero on number | { "age": 1 } |
{ "note": undefined } | undefined is not valid | { "note": null } |
Spaces, tabs, and newlines are ignored in JSON. Use them to make your data readable. The example above is valid with no whitespace too:
{"name":"Alice","age":30,"city":"New York"}
This is called minified JSON. It is smaller for transfer but hard to read. Use pretty-printed JSON (with indentation) when editing by hand.
Every piece of information in JSON has a key (the label) and a value (the actual data).
✅ Correct
{
"country": "Canada"
}
❌ Wrong
{
"country"
}
Why is it wrong? The value is missing. Every key must have a value.
All keys in JSON must use double quotation marks (").
✅ Correct
{
"animal": "Tiger"
}
❌ Wrong
{
animal : "Tiger"
}
Why is it wrong? The key animal is not inside double quotes.
A value can be:
✅ Correct
{
"temperature": 28,
"isRaining": false
}
❌ Wrong
{
"temperature":
}
Why is it wrong? Every key must have a valid value.
When an object has more than one key-value pair, separate them with commas.
✅ Correct
{
"brand": "Toyota",
"year": 2024
}
❌ Wrong
{
"brand": "Toyota"
"year": 2024
}
Why is it wrong? There is no comma between the two key-value pairs.
The last key-value pair should not end with a comma.
✅ Correct (Single Key)
{
"language": "English"
}
❌ Wrong (Single Key)
{
"language": "English",
}
Why is it wrong? JSON does not allow a comma after the last item.
✅ Correct (Two Keys)
{
"country": "USA",
"language": "English"
}
❌ Wrong (Two Keys)
{
"country": "USA",
"language": "English",
}
Why is it wrong? The second key-value pair has a trailing comma. The last item should never end with a comma, even when there are multiple items.
A JSON object must begin with { and end with }.
✅ Correct
{
"city": "Tokyo"
}
❌ Wrong
[
"city": "Tokyo"
]
Why is it wrong? Square brackets are used for arrays, not objects.
A JSON array stores a list of values and must use [ ].
✅ Correct
[
"Red",
"Green",
"Blue"
]
❌ Wrong
{
"Red",
"Green",
"Blue"
}
Why is it wrong? A list of values should be inside square brackets, not curly braces.
Spaces, tabs, and blank lines do not change the meaning of JSON. They only make it easier for people to read.
✅ Easy to Read (Pretty-Printed JSON)
{
"school": "Green Valley",
"students": 450
}
✅ Also Correct (Minified JSON)
{"school":"Green Valley","students":450}
Both examples contain exactly the same data. The first version is easier for people to read, while the second version is smaller and is often used when sending data over the internet.