{}JSON FYI

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.

·5 min read

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 →

Frequently asked questions

Can a JSON array contain different types?+

Yes. JSON arrays are heterogeneous — you can mix strings, numbers, booleans, null, objects, and nested arrays freely. Whether that's a good design for your API is a separate question.

Is an empty JSON array [] valid?+

Yes. An empty array [] is perfectly valid JSON. It serialises to the string '[]' and deserialises to an empty list/array in any language.

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

Arrays are ordered lists accessed by zero-based numeric index, written with square brackets []. Objects are unordered collections of key-value pairs accessed by string key, written with curly braces {}. Arrays are typically used for lists of similar items; objects for structured records.

Does JSON preserve array order?+

Yes. JSON arrays are ordered sequences. The specification guarantees that parsers must preserve element order. This is unlike JSON objects, where key order is not guaranteed by the spec (though most parsers preserve insertion order).

Related tools & guides