Just to add some additional information — in that example, the JSON contains an array of books: "books":[...] This approach works well when you have many books. However, it's not necessary to store data in arrays when using JSON. In fact, I rarely do that.
For example, if you want to store stats for different weapons in an RPG game, using an array means you'll have to iterate through it every time you need to find a specific weapon:
{
"weapons": [
{
"name": "Short Sword",
"type": "melee",
"damage": 8,
"range": 1,
"weight": 3
},
{
"name": "Longbow",
"type": "ranged",
"damage": 12,
"range": 10,
"weight": 4
},
{
"name": "Fire Staff",
"type": "magic",
"damage": 15,
"range": 6,
"weight": 5
}
]
}
To find a weapon you will need to loop thought the array:
JSON For Each element in "weapons"
JSON compare ".name"="Short Sword"
It's much easier to store each weapon as a separate object instead:
{
"short_sword": {
"type": "melee",
"damage": 8,
"range": 1,
"weight": 3
},
"longbow": {
"type": "ranged",
"damage": 12,
"range": 10,
"weight": 4
},
"fire_staff": {
"type": "magic",
"damage": 15,
"range": 6,
"weight": 5
}
}
Then you can access any weapon and its stats directly with a single expression, for example:
JSON.get("fire_staff.damage")
And of course objects can contain nested objects and arrays: