JSON Getting started with JSON JSON Array


Example

A JSON Array is an ordered collection of values. It is surrounded by square braces i.e [], and values are comma-delimited:

{ "colors" : [ "red", "green", "blue" ] }

JSON Arrays can also contain any valid JSON element, including objects, as in this example of an array with 2 objects (taken from the RFC document):

[
  {
     "precision": "zip",
     "Latitude":  37.7668,
     "Longitude": -122.3959,
     "Address":   "",
     "City":      "SAN FRANCISCO",
     "State":     "CA",
     "Zip":       "94107",
     "Country":   "US"
  },
  {
     "precision": "zip",
     "Latitude":  37.371991,
     "Longitude": -122.026020,
     "Address":   "",
     "City":      "SUNNYVALE",
     "State":     "CA",
     "Zip":       "94085",
     "Country":   "US"
  }
]

They can also contain elements with mixed types, for example:

[
    "red",
    51,
    true,
    null,
    {
        "state": "complete"
    }
]

A common mistake when writing JSON arrays (and objects) is to leave a trailing comma after the last element. This is common pattern in many languages, but unfortunately isn't valid in JSON. For example, the following array is invalid:

[
    1,
    2,
]

To make this valid, you would need to remove the comma after the last element, turning it into:

[
    1,
    2
]