Chapter 3

JSON Arrays

Learn how to use and manipulate JSON arrays

What is a JSON Array?

A JSON array is an ordered list of values. Data is enclosed in [], and elements are separated by commas ,. Elements in an array can be any JSON data type.

{
  "fruits": ["apple", "banana", "orange"],
  "numbers": [1, 2, 3, 4, 5],
  "mixed": [1, "two", true, null, {"key": "value"}]
}

Array Features

1 Ordered Collection

Elements maintain insertion order and can be accessed via index (0-based).

2 Any Data Type

Array elements can be strings, numbers, booleans, objects, arrays, or any valid JSON type.

3 Nestable

Arrays can contain other arrays or objects, forming complex data structures.

4 Can be Empty

Empty arrays [] are valid JSON.

Common Array Operations

Array of Objects

Arrays of objects are one of the most common data structures, often used to represent lists:

{
  "users": [
    {
      "id": 1,
      "name": "John Doe",
      "age": 28
    },
    {
      "id": 2,
      "name": "Jane Smith",
      "age": 32
    },
    {
      "id": 3,
      "name": "Bob Johnson",
      "age": 25
    }
  ]
}

💡 Tip: In practice, API responses are often in the format of an array of objects.