08 Arrays

A JSON array is used to store a list of values.

Use an array whenever you have more than one value for the same piece of information.

For example, if a student has several favorite subjects, you can store them in an array.

{
    "favoriteSubjects": [
        "Math",
        "Science",
        "Art"
    ]
}

Here, favoriteSubjects contains three values, so an array is used.

Arrays Can Store Objects

An array can contain objects. This is useful when you have a list of similar items.

For example, a classroom may have many students.

{
    "students": [
        {
            "name": "Emma",
            "grade": 8
        },
        {
            "name": "Noah",
            "grade": 9
        }
    ]
}

Here:

  • students is an array.
  • Each student is stored as an object.

Arrays Can Contain Other Arrays

Arrays can also contain other arrays.

For example, each row of seats in a classroom can be stored as an array.

{
    "seating": [
        ["Anna", "Ben"],
        ["Chris", "David"]
    ]
}

Each inner array represents one row of seats.

Rules for JSON Arrays

Rule 1: Arrays Use Square Brackets [ ]

Every array must start with [ and end with ].

✅ Correct

[
    "Red",
    "Green",
    "Blue"
]

❌ Wrong

{
    "Red",
    "Green",
    "Blue"
}

Why is it wrong? Lists of values must use square brackets, not curly braces.

Rule 2: Separate Values with Commas

Each value in an array must be separated by a comma.

✅ Correct

[
    "Dog",
    "Cat",
    "Rabbit"
]

❌ Wrong

[
    "Dog"
    "Cat"
    "Rabbit"
]

Why is it wrong? The values are missing commas.

Rule 3: Arrays Can Be Empty

An array does not have to contain any values.

✅ Correct

[]

This is called an empty array.

For example:

{
    "notifications": []
}

This means there are currently no notifications.

Rule 4: Arrays Can Contain Different Data Types

An array can store strings, numbers, booleans, objects, arrays, or even null.

✅ Example

[
    "Apple",
    25,
    true,
    null
]

Although this is valid JSON, it is usually better to store similar types of data together to keep your data organized.

Rule 5: Arrays Can Be Nested

An array can contain another array or an object.

Array inside an array

[
    ["A", "B"],
    ["C", "D"]
]

Object inside an array

[
    {
        "name": "Leo"
    },
    {
        "name": "Mia"
    }
]

Accessing Array Values Using an Index

Each item in an array has a position called an index.

JSON itself does not define indexing. However, when arrays are used in programming languages such as JavaScript or Python, indexing usually starts at 0.

For example:

[
    "Apple",
    "Banana",
    "Orange"
]
IndexValue
0"Apple"
1"Banana"
2"Orange"

So:

  • Index 0 → "Apple"
  • Index 1 → "Banana"
  • Index 2 → "Orange"

Note: JSON is only a data format. The programming language you use to read JSON decides how array indexing works. Most modern programming languages start counting from 0.

Summary

Use an array whenever you need to store multiple values.

Examples include:

  • A list of students
  • Favorite foods
  • Subjects
  • Phone numbers
  • Countries visited

Remember

  • ✅ Arrays use square brackets [ ].
  • ✅ Values are separated by commas.
  • ✅ Arrays can be empty ([]).
  • ✅ Arrays can contain objects, other arrays, or different value types.
  • ✅ In most programming languages, array indexing starts at 0.