JSON Array Example: How Arrays Work in JSON
Explore JSON arrays through practical examples — from simple lists to complex nested structures. Learn the syntax rules and when to reach for arrays instead of objects.
What Is a JSON Array?
A JSON array is an ordered list of values enclosed in square brackets []. Values inside an array are separated by commas. Unlike objects, arrays don't have keys — each element is accessed by its position (index), starting from zero. Arrays can hold any valid JSON type: strings, numbers, booleans, null, objects, and even other arrays.
Array of Strings
The simplest array is a flat list of values of the same type:
{
"fruits": ["apple", "banana", "cherry", "dragonfruit", "elderberry"]
}This is the pattern you'll use most often — a named property whose value is an array of strings. Tags, categories, and roles are typical use cases.
Array of Numbers
Number arrays are common for datasets, scores, and coordinates:
{
"temperatures": [72.5, 68.3, 75.1, 80.0, 69.9],
"coordinates": [40.7128, -74.0060]
}Remember that JSON numbers are never quoted. Writing "72.5" makes it a string, not a number.
Array of Objects
This is the most powerful pattern — and the one you'll encounter in virtually every API response. Each element in the array is a full JSON object:
{
"employees": [
{
"id": 1,
"name": "Alice Chen",
"department": "Engineering",
"skills": ["TypeScript", "React", "Node.js"]
},
{
"id": 2,
"name": "Bob Martinez",
"department": "Design",
"skills": ["Figma", "CSS", "Illustration"]
},
{
"id": 3,
"name": "Carol Johnson",
"department": "Marketing",
"skills": ["Analytics", "SEO", "Content Strategy"]
}
]
}Each object in the array typically has the same structure, making arrays of objects ideal for representing collections of similar entities like users, products, or records.
Mixed-Type Arrays
JSON arrays can contain values of different types. While technically valid, this is generally discouraged because it makes the data harder to process:
{
"mixedData": ["hello", 42, true, null, {"key": "value"}, [1, 2, 3]]
}Every element above is a different type — string, number, boolean, null, object, and array. Most typed languages and schema validators expect homogeneous arrays, so use mixed-type arrays sparingly and only when the schema genuinely calls for it.
Nested Arrays
Arrays can contain other arrays, which is useful for matrix data, grids, or grouped lists:
{
"ticTacToe": [
["X", "O", "X"],
["O", "X", "O"],
["O", "X", "X"]
],
"schedule": [
["Monday", "9:00 AM", "Team Standup"],
["Wednesday", "2:00 PM", "Sprint Review"],
["Friday", "11:00 AM", "Retrospective"]
]
}A two-dimensional array like ticTacToe above is an array of arrays. You access a specific cell using two indices — for example, ticTacToe[0][2] returns "X".
When to Use Arrays vs Objects
Choosing between an array and an object depends on the nature of your data:
- Use an array when you have a list of items where order matters or where items are accessed by position. Examples: search results, timeline events, coordinates.
- Use an object when each piece of data has a meaningful name and you want to access it by key. Examples: user profile, configuration settings, API error details.
- Use an array of objects when you have a collection of entities that each have named properties. This is the most common pattern in practice.
Common Mistakes to Avoid
- Trailing comma after the last element:
["a", "b", "c",]is invalid JSON. Remove the trailing comma. - Missing commas between elements:
["a" "b"]will cause a parse error. Every element needs a comma separator. - Using an array when an object is more appropriate:If your elements have named properties, don't use a flat array like
["Jane", 29, "Portland"]. Use an object with named keys instead. - Assuming arrays are unordered: JSON arrays preserve order. If order matters in your data, arrays are the right choice.
Empty Arrays
An empty array []is perfectly valid. It's common in API responses when a query returns no results:
{
"searchResults": [],
"totalCount": 0
}Returning an empty array instead of null is generally better practice — it lets the consumer iterate without a null check.
Try it yourself: Paste this example into our JSON Formatter to see it beautified and validated instantly.