Array in JSON

JSON array is used to store values of the similar type. Square brackets are used to store JSON array. The comma must separate values inside a JSON array. You can store numbers, string, boolean, object, and even another array inside a JSON array.

How to use Strings in JSON Array

It's always better to understand a topic with the help of a valid example.

{
	"name": "Moto G5 Plus",
	"price": 17000,
	"color": ["lunar gray", "fine gold"],
	"inStock" :true,
	"features": {
		"screen size": 5.2,
		"operating system": "android 7.0",
		"wireless charging": false,
		"connectivity": ["wi-fi", "4G LTE", "bluetooth"]
	}
}

Here, color of Motorola G5 is stored in a JSON array in a key "color".

How to use Numbers in JSON Array

Let's see how we can use numbers in JSON array with the help of below example-

{
	"productId": 53216,
	"name": "Reebok Athletic Shoes",
	"description": "A pair of shoes that are designed for athletic championships",
	"price": 2000,
	"sizes": [6, 7, 8, 9, 10, 11],
	"inStock": true
}

Here, the sizes of shoes are stored in the JSON array in a key "sizes".

How to use Objects in JSON Array

JSON allows you can to create an array of objects in the following way-

{
	"name": "Black T-Shirt",
	"price": 300,
	"sizes": [32, 34, 36, 38],
	"inStock": true,
	"seller": [{
			"name": "Stylish Shop",
			"rating": 5.0
 	  	},
	  	{
			"name": "Trendy Dresses Shop",
			"rating": 4.5
		}
	]
}

Here, the seller of T-Shirt are stored in the JSON array in a key "seller".

Nested JSON Array

As a programmer, you might encounter a situation in which you need to store data as a nested array. For example, you need to store the sizes of each color of kurtis, in that case, use nested JSON array.

{
	"name": "Biba Kurtis",
	"price": 1500,
	"color": ["yellow", "white"],
	"inStock": true,
	"sizes": [["S", "M", "L", "XL", "2XL"],
		["L", "XL", "2XL"]
	]
}