JSON Array Tutorial

Last updated:

A JSON array is an ordered, zero-indexed list enclosed in square brackets [] — 1 of the 2 structural types in JSON. Arrays hold any of the 6 JSON value types (string, number, boolean, null, object, nested array) and always preserve insertion order. REST APIs return arrays of objects in over 80% of list endpoints; parsing a 1,000-element array with JSON.parse() takes under 5 ms in V8. This guide covers array syntax and validation, zero-based index access, nested array traversal, arrays of objects, filtering with .filter() and .find(), and converting arrays to CSV.

Convert JSON arrays to CSV online

Paste a JSON array of objects to download a formatted CSV file instantly — no sign-up required.

Open JSON to CSV Converter

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.

Frequently asked questions

What is a JSON array?

A JSON array is an ordered list of values enclosed in square brackets, with values separated by commas. It can contain strings, numbers, booleans, null, objects, and nested arrays. Order is preserved. The most common API pattern is an array of objects representing a list of records.

Can JSON arrays mix different types?

Yes — a single array can mix strings, numbers, booleans, null, and objects. This is syntactically valid JSON. In practice, most APIs use homogeneous arrays (all elements the same type) for easier programmatic processing.

How do I access a JSON array element in JavaScript?

After JSON.parse(), use zero-based numeric index: data.fruits[0] for the first element. Chain indexes for nested arrays. Use array.at(-1) for the last element. Optional chaining (data?.fruits?.[0]) prevents errors on undefined arrays.

What is the difference between a JSON array and JSON object?

Arrays are ordered, indexed collections in square brackets; objects are named key-value pairs in curly braces. Use arrays for lists of items; use objects for entities with named properties. Arrays use numeric access; objects use string key access.

How do I loop over a JSON array?

In JavaScript: for...of, forEach, map, filter, find. In Python: standard for item in data loop or list comprehension. All standard iteration methods work the same after parsing.

What is the maximum size of a JSON array?

The JSON spec sets no limit. JavaScript's V8 supports up to 2^32-1 elements, but memory is the real limit. APIs typically paginate large collections. For very large files, use streaming JSON parsers to avoid loading everything into memory at once.