JSON arrays — syntax, examples, and common mistakes
How JSON arrays work: syntax rules, nesting, mixed types, accessing elements in JavaScript and Python, and the mistakes that cause parse errors.
A JSON array is an ordered list of values enclosed in square brackets. Arrays can contain any mix of JSON value types and are zero-indexed like arrays in most programming languages.
Syntax rules
[value, value, value]
- Square brackets wrap the array: [ and ]
- Values are separated by commas
- No trailing comma after the last value
- Values can be any JSON type: string, number, boolean, null, object, or another array
- Whitespace (spaces, tabs, newlines) between values is allowed and ignored
Examples
// Array of strings
["apple", "banana", "cherry"]
// Array of numbers
[1, 2, 3, 4, 5]
// Mixed types (valid JSON)
["Ada", 30, true, null, {"role": "engineer"}]
// Empty array
[]
// Nested arrays (matrix)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
// Array of objects (the most common API pattern)
[
{"id": 1, "name": "Ada"},
{"id": 2, "name": "Linus"},
{"id": 3, "name": "Grace"}
]Accessing array elements
// JavaScript
const tags = JSON.parse('["json", "api", "rest"]');
tags[0]; // "json"
tags.length; // 3
tags[tags.length - 1]; // "rest"
// Nested
const matrix = JSON.parse('[[1,2],[3,4]]');
matrix[1][0]; // 3
// Python
import json
tags = json.loads('["json", "api", "rest"]')
tags[0] # "json"
tags[-1] # "rest" (Python negative indexing)Common mistakes
Trailing comma
// ✗ Invalid — trailing comma after last element [1, 2, 3,] // ✓ Valid [1, 2, 3]
Using single quotes
// ✗ Invalid ['apple', 'banana'] // ✓ Valid — double quotes only ["apple", "banana"]
Missing comma between elements
// ✗ Invalid ["apple" "banana"] // ✓ Valid ["apple", "banana"]
Arrays vs objects in JSON APIs
Use an array when the response is a list of items with no meaningful key. Use an object when you need named fields or metadata alongside the list.
// Array response — a list of users
[{"id":1,"name":"Ada"}, {"id":2,"name":"Linus"}]
// Object response — metadata + data
{
"total": 2,
"page": 1,
"users": [{"id":1,"name":"Ada"}, {"id":2,"name":"Linus"}]
}Paste your JSON into the validator →
Get the exact line, column, and a fix hint in seconds — no upload, no signup.
Open JSON Validator →