JSON Array Examples
JSON arrays are used to store a collection of values in a structured and organized manner. A JSON array is represented as a list of values within square brackets ([]) and separated by commas. Each value in the array can be of a different data type, such as a string, number, or object.
Here are some examples of JSON arrays:
[1, 2, 3, 4, 5]
["apple", "banana", "cherry"]
[true, false, true]
[{"name":"John", "age":30}, {"name":"Jane", "age":25}]
The first example is a JSON array of numbers. The second example is a JSON array of strings. The third example is a JSON array of booleans. The fourth example is a JSON array of objects, where each object has a "name" and "age" property.
JSON arrays can be used to represent a variety of data structures, such as lists, sets, and sequences. They can also be used to store multiple pieces of related data, such as a list of students and their respective information.
JSON arrays are commonly used in combination with JSON objects to create more complex data structures. For example, consider the following JSON object:
[
{
"id":1,
"name":"Red Pillow",
"price":20.99,
"category":"home decor",
"rating":4.5,
"reviews":[
{
"author":"Alice",
"stars":5,
"comment":"I love this pillow! It's so soft and comfortable. The red color is vibrant and adds a nice pop of color to my living room."
},
{
"author":"Bob",
"stars":3,
"comment":"The pillow is okay, but I wish it was a bit bigger. It's a little too small for my liking."
}
]
},
{
"id":2,
"name":"Blue T-Shirt",
"price":14.99,
"category":"clothing",
"rating":4.8,
"reviews":[
{
"author":"Charlie",
"stars":5,
"comment":"I love this t-shirt! The blue color is so bright and the material is very soft and comfortable. I'll definitely be buying more in different colors."
},
{
"author":"Diane",
"stars":4,
"comment":"The t-shirt is nice, but it does shrink a bit in the wash. Just make sure to follow the care instructions."
}
]
}
]
In this example, the JSON array contains objects that represent products. Each object has several properties, such as "id", "name", "price", and "category". The "rating" property is a decimal value, and the "reviews" property is a JSON array of objects representing customer reviews. Each review object has properties for the author, number of rated stars, and comment.