01 Basic Rules

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.

Basic JSON Terms

Key-Value Pair

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.

Object

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.

Array

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:

  • Favorite foods
  • Subjects
  • Phone numbers

Fundamental Rules

RuleDescription
Data is in name/value pairsA key followed by a colon and a value: "key": value
Keys must be stringsKeys must be wrapped in double quotes: "name" (not name)
Values have typesA value can be a string, number, boolean, null, object, or array
Commas separate pairsEach 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

Basic Structure

Every JSON document starts and ends with either { } (an object) or [ ] (an array).

{
    "name": "Alice",
    "age": 30,
    "city": "New York"
}

What is NOT allowed

InvalidWhyFix
{ 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 }

Whitespace

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.

Examples

Rule 1: Data is Stored as Key-Value Pairs

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.

Rule 2: Keys Must Be Written Inside Double Quotes

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.

Rule 3: Values Can Have Different Data Types

A value can be:

  • A string (text)
  • A number
  • A boolean (true or false)
  • null
  • An object
  • An array

✅ Correct

{
    "temperature": 28,
    "isRaining": false
}

❌ Wrong

{
    "temperature": 
}

Why is it wrong? Every key must have a valid value.

Rule 4: Separate Items with Commas

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.

Rule 5: Do Not Add a Comma After the Last Item

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.

Rule 6: Objects Use Curly Braces

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.

Rule 7: Arrays Use Square Brackets

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.

Whitespace in JSON

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.