Here's the JSON data we'll use in this example:
Here's the code:
In this example, we output the formatted JSON data using
window.onload. Alternatively, you could use some other event to call the function.
Nested For Loop
Here's another example. This time we'll use a larger JSON file with deeper nesting.
Not only does this file have artist information, but each artist has a number of albums. So we'll need to loop through the artists, and also loop through each artists' albums.
Just like in the first example, this example uses a function that is called when the page loads.
However, the function in the second example uses two
for loops — one nested inside the other — to retrieve the nested data.