TExcelTAD
Home › JSON › JSON Arrays

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.

json · try it yourself
["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:

json · try it yourself
[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:

json · try it yourself
{
  "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:

js · try it yourself
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

js · try it yourself
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:

json · try it yourself
{
  "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?

  1. arr[2]
  2. arr[1]
  3. arr.second
  4. arr(1)

Which is a valid JSON array?

  1. [1, 2, 3,]
  2. (1, 2, 3)
  3. [1, "two", null]
  4. {1, 2, 3}

Which structure fits 'a list of 50 customers' best?

  1. An object with keys "customer1"…"customer50"
  2. An array of customer objects
  3. A string with commas
  4. 50 separate JSON documents