As seen above, there are a few rules to JSON’s syntax. They are that data should be in name and value pairs, data is separated by commas, objects should be held in curly braces, and as always, arrays are held in square brackets. The data types that can be used with JSON include strings (JSON format requires double quotes!), numbers, objects, arrays, Booleans, and null.
Retrieving Info from JSON Objects
JSON object values are typically accessed using dot (.) notation or with brackets. Here we set the object to a variable named assassin then declare that to a variable called x so we can access it:
We can access JSON objects by looping through them using a for-in loop. Assuming we have an element in our html file with an id set to ”asset-info”, we can get properties or values about our assassin like so:
Nested JSON objects can be accessed just as easily using either dot notation or bracket notation:
Retrieving Info from JSON Arrays
Arrays in JSON are accessed like your average array. The most basic way of course is using its index number in brackets. Below is an example of using the array’s index to retrieve a single value and retrieving all values using a for loop.
Using JSON to grab data can seem confusing at first. As with most things, learning the basics will go a long way to help build our skills. A lot of times complicated things can be broken down into the basics and will turn out to not be that difficult to solve after all. I hope this guide will help someone out. Happy coding!