Understanding JSON Arrays: Working with Ordered Lists of JSON Data
Learn about JSON arrays, ordered lists of values used in JSON data structures. This guide explains JSON array syntax, data types allowed within arrays, nested arrays, and provides examples illustrating their use in representing structured data.
Understanding JSON Arrays
What is a JSON Array?
A JSON array is an ordered list of values. Unlike JSON objects, which use key-value pairs, arrays simply contain a sequence of items. These items can be of various data types: strings, numbers, booleans, or even other JSON objects (nested objects).
Arrays are enclosed in square brackets []
, and items are separated by commas.
Examples of JSON Arrays
Here are examples of JSON arrays with different data types:
JSON Array of Strings
Code
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
JSON Array of Numbers
Code
[12, 34, 56, 43, 95]
JSON Array of Booleans
Code
[true, true, false, false, true]
JSON Array of Objects
Code
{
"employees": [
{ "name": "Ram", "email": "ram@gmail.com", "age": 23 },
{ "name": "Shyam", "email": "shyam23@gmail.com", "age": 28 },
{ "name": "John", "email": "john@gmail.com", "age": 33 },
{ "name": "Bob", "email": "bob32@gmail.com", "age": 41 }
]
}
Multidimensional JSON Array
You can create nested arrays (arrays within arrays):
Code
[
["a", "b", "c"],
["m", "n", "o"],
["x", "y", "z"]
]