JSON Arrays
A JSON array is an ordered list of values inside square brackets. Unlike objects, the order of array items is meaningful: the first item is always the first.
["Hanoi", "Hue", "Da Nang", "Ho Chi Minh City"]Valid JSON: array with 4 items
[
"Hanoi",
"Hue",
"Da Nang",
"Ho Chi Minh City"
]
Array rules
- Starts with
[and ends with]. - Items are separated by commas, with no trailing comma.
- Items can be any JSON value: strings, numbers, booleans,
null, objects or other arrays. - An empty array
[]is valid.
Mixed types
JSON allows different types in the same array:
[1, "two", true, null, {"five": 5}, [6]]Valid JSON: array with 6 items
[
1,
"two",
true,
null,
{
"five": 5
},
[
6
]
]
Allowed, but usually a bad idea for real data — code that consumes the array has to handle every type. Prefer arrays where every item has the same shape.
Arrays of objects
The most common pattern in APIs is a list of records, each an object with the same keys:
{
"orders": [
{"id": 101, "customer": "Binh", "total": 39.0},
{"id": 102, "customer": "Chi", "total": 12.5},
{"id": 103, "customer": "Dung", "total": 88.25}
]
}Valid JSON: object with 1 key
{
"orders": [
{
"id": 101,
"customer": "Binh",
"total": 39
},
{
"id": 102,
"customer": "Chi",
"total": 12.5
},
{
"id": 103,
"customer": "Dung",
"total": 88.25
}
]
}
Working with arrays in JavaScript
A parsed JSON array is a normal JavaScript array, so all array methods work:
const data = JSON.parse(`{
"orders": [
{"id": 101, "customer": "Binh", "total": 39.0},
{"id": 102, "customer": "Chi", "total": 12.5},
{"id": 103, "customer": "Dung", "total": 88.25}
]
}`);
const orders = data.orders;
console.log(orders.length);
console.log(orders[0].customer); // first item
console.log(orders[orders.length - 1].id); // last item
console.log(orders.at(-1).id); // same, with at()
const big = orders.filter((o) => o.total > 20).map((o) => o.id);
console.log(big);
const sum = orders.reduce((acc, o) => acc + o.total, 0);
console.log("Sum:", sum);3
Binh
103
103
[ 101, 103 ]
Sum: 139.75
Looping
const tags = JSON.parse('["json", "api", "web"]');
for (const tag of tags) {
console.log("#" + tag);
}
tags.forEach((tag, i) => console.log(i, tag));#json
#api
#web
0 json
1 api
2 web
Nested arrays
Arrays can hold arrays — handy for grids, matrices or coordinate pairs:
{
"board": [
["X", "O", "X"],
[null, "X", "O"],
["O", null, "X"]
]
}Valid JSON: object with 1 key
{
"board": [
[
"X",
"O",
"X"
],
[
null,
"X",
"O"
],
[
"O",
null,
"X"
]
]
}
In JavaScript you would read the centre square as data.board[1][1].
Objects vs arrays: which one?
| Use an array when... | Use an object when... |
|---|---|
| Order matters | You look items up by name |
| Items are all the same kind | Each key means something different |
| The count varies | The set of keys is fixed |
| Example: a list of products | Example: one product's fields |
Tip: A common mistake is using an object with numbered keys, like
{"0": "a", "1": "b"}. If the keys are just positions, use an array.
Test yourself
How do you get the second item of a parsed JSON array arr in JavaScript?
- arr[2]
- arr[1]
- arr.second
- arr(1)
Which is a valid JSON array?
- [1, 2, 3,]
- (1, 2, 3)
- [1, "two", null]
- {1, 2, 3}
Which structure fits 'a list of 50 customers' best?
- An object with keys "customer1"…"customer50"
- An array of customer objects
- A string with commas
- 50 separate JSON documents