JSON syntax: what do square brackets mean?
In JSON, square brackets [ ] surround an array: an ordered list of values. This page shows how to read that list, put it inside an object, and fix the mistakes that often appear around brackets.
What do square brackets mean in JSON?
Square brackets mean “array.” Everything between the opening [ and closing ] is an item in the list. Commas separate items. The first item is at position 0 when a program reads the array.
An array of three strings
["Asha", "Luis", "Mina"]This array has three values in order: "Asha", "Luis", and "Mina". It is valid JSON by itself. An empty array is [].
Square brackets [ ] versus curly braces { }
Use square brackets for a list of values. Use curly braces for an object with named fields. The two often work together: an object has a key whose value is an array.
An object containing an array of objects
{
"people": [
{ "name": "Asha", "age": 28 },
{ "name": "Luis", "age": 31 }
]
}The outer { } is an object. Its "people" key points to an array between [ ]. Each item in that array is another object with "name" and "age" fields. If you need a table with one row per person, this array of objects is a natural shape.
| Syntax | Represents | Use it when |
|---|---|---|
["red", "blue"] | An array | You have values in a sequence |
{"color": "red"} | An object | You need a named field |
{"colors": ["red", "blue"]} | An object with an array | You need a named list |
What can go inside a JSON array?
An array may contain strings, numbers, true, false, null, objects, or more arrays. JSON does not require every item to have the same type. In data you plan to process as rows, keeping items the same shape usually makes the result easier to work with.
Nested arrays
[
[1, 2],
[3, 4]
]This outer array contains two inner arrays. Each inner array has two numbers. The brackets must close from the inside out.
The three rules to remember
- Open with
[and close with]. - Put a comma between items. Do not add a comma after the last item.
- Each item must itself be a valid JSON value. For example, text needs double quotes:
["yes", "no"].
Spaces and line breaks can make an array easier to read, but they do not create separators. The comma is still required.
Common square bracket errors
The following three snippets are invalid JSON. The comments label the problems for this lesson; comments themselves are also not allowed in JSON files.
// Missing comma between items
["Asha" "Luis"]
// Trailing comma after the last item
["Asha", "Luis",]
// Wrong closing mark
["Asha", "Luis"}Fix the first by adding a comma, the second by removing the last comma, and the third by changing } to ]. The corrected array is:
["Asha", "Luis"]Should the whole JSON document start with [ or {?
Start with [ when the document is a list, such as many products or many people. Start with { when it describes one thing with named fields, such as a single product or a settings object. Both are valid JSON. A document can also be one simple JSON value.
For the rest of the format, including .json files, double quotes, comments, and XML comparisons, read the complete beginner’s guide to JSON.
Source
The array grammar and permitted JSON values come from RFC 8259, section 5: Arrays.