Before we explore what APIs are and how to use them. You first need to be introduced to a new data structure; JSON or JavaScript Object Notation.
JSON is a standardized method for storing and transporting unordered collections of data. This data is stored through name:value pairs. Furthermore, the values in JSON data can be;
JSON Objects, once in JavaScript (i.e. after being sent over from an API on a remote server), appear as JS objects. As such, they can be traversed and manipulated using standard JS object notation and techniques.
As an example, the following code creates a JS Object, which is also valid as JSON.
let obj = {
"someName" : "someValue",
"firstName" : "Michael",
"aPerson" : {
"firstName" : "Mr.",
"lastName" : "Peanutbutter"
},
"anArray" : [
"zero",
"one",
"two"
]
}
Once this object is binded to the variable obj
, we can navigate through it and access it unique values.
The following example creates the same object as above, but also demonstrates how to access values within the object.