JSON Array Tutorial

A JSON array is an ordered list of values enclosed in square brackets. Arrays are one of the two compound data structures in JSON (the other being objects) and are used to represent lists, collections, and sequences of data.

Basic array syntax

Arrays use square brackets, with values separated by commas:

["apple", "banana", "cherry"]
[1, 2, 3, 4, 5]
[true, false, null]

Mixed-type arrays

JSON arrays can contain any mix of types — strings, numbers, booleans, null, objects, and even other arrays. There is no requirement for all elements to be the same type.

[42, "hello", true, null, {"key": "value"}, [1, 2]]

In practice, most APIs return arrays where all elements share the same structure, making them easier to process.

Array of objects (most common pattern)

The most common use of JSON arrays in APIs is a list of objects. Each object represents one record or item:

{
  "users": [
    { "id": 1, "name": "Alice", "role": "admin" },
    { "id": 2, "name": "Bob",   "role": "user"  },
    { "id": 3, "name": "Carol", "role": "user"  }
  ]
}

Nested arrays

Arrays can contain other arrays (multidimensional data):

{
  "matrix": [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
  "coordinates": [[40.7128, -74.0060], [51.5074, -0.1278]]
}

Empty array

An empty array is valid JSON:

{ "tags": [] }

Access array elements in JavaScript

After parsing with JSON.parse(), use zero-based index to access elements:

const json = '{"fruits": ["apple", "banana", "cherry"]}';
const data = JSON.parse(json);

console.log(data.fruits[0]);  // "apple"
console.log(data.fruits[2]);  // "cherry"
console.log(data.fruits.length);  // 3

Iterate over a JSON array in JavaScript

const json = '[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]';
const users = JSON.parse(json);

// for...of loop
for (const user of users) {
  console.log(user.id, user.name);
}

// forEach
users.forEach(user => {
  console.log(user.name);
});

// map — transform each element
const names = users.map(user => user.name);
console.log(names);  // ["Alice", "Bob"]

// filter — keep only matching elements
const admins = users.filter(user => user.role === "admin");

// find — first match
const alice = users.find(user => user.name === "Alice");

Access array elements in Python

import json

json_string = '{"fruits": ["apple", "banana", "cherry"]}'
data = json.loads(json_string)

print(data["fruits"][0])   # apple
print(data["fruits"][-1])  # cherry (last element)
print(len(data["fruits"])) # 3

Iterate over a JSON array in Python

import json

json_string = '[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]'
users = json.loads(json_string)

# for loop
for user in users:
    print(user["id"], user["name"])

# List comprehension
names = [user["name"] for user in users]

# filter
admins = [u for u in users if u.get("role") == "admin"]

Common JSON array mistakes

  • Trailing comma["a", "b",] is invalid JSON. Remove the comma after the last element.
  • Single quotes — String values must use double quotes:["apple"] not ['apple'].
  • Using objects when arrays are needed — Use an array when order matters or when you have multiple items of the same type.

Validate and explore JSON arrays

Use the JSON Formatter's tree view to visually explore nested arrays, or the JSONPath Tester to query specific elements with expressions like $.users[*].name.