1. Introduction to APIs and JSONs
In this chapter, you'll explore pulling data from the web even further by
2. APIs
learning how to interact with APIs, or Application Programming Interfaces. An API is a set of protocols and routines for building and interacting with software applications. In particular, you'll learn how to use the Open Movie Database API and, in the next chapter,
3. APIs
the Twitter API to pull data from both applications, while learning about API interaction best practices.
A standard form for transferring data through APIs is
4. JSONs
the JSON file format, so in this video, we'll focus our attention squarely on these. Then we'll move onto actually getting data from APIs.
JSON is an acronym that is short for JavaScript Object Notation. It is a file format that arose out of a growing need for real-time server-to-browser communication that wouldn't necessarily rely on Flash or Java and was first specified and also popularized by Douglas Crockford, an American programmer and entrepreneur.
One of the cool things about JSONs is that they're human readable, that is, they can naturally be read by humans unlike, for example, pickled files, as we saw in the previous course. As they're human readable, let's check one out!
5. JSONs
Here you see a JSON from the OMDB OR Open Movie Database API. In particular, this is JSON containing information about the movie Snakes on a Plane.
First notice that the JSON consists of name-value pairs separated by commas. This will remind you of the key-value pairs in a Python dictionary! We'll see in a minute that, for this reason, when loading JSONs into Python, it is natural to store them in a dict.
The keys in JSONs will always be strings enclosed in quotation marks. The values can be strings, integers, arrays or even objects. Such an object can even be a JSON and then you have nested JSONs but we won't go further into these here.
In this case of the Snakes on a Plane JSON, all the values are strings and we can see this from the quotation marks.
6. JSONs
The value corresponding to the key 'Title' is the title of the movie as a string: Snakes on a Plane.
7. JSONs
The value corresponding to the key 'Year' is the year of release as a string: 2006 and so on. There's the rating, the runtime, director, writers, plot, language and much more! You'll soon learn how to use the OMDB API and Python to automate retrieval of such data, but first you'll figure out how to load JSONs from a local directory.
8. Loading JSONs in Python
Lets say that I had the JSON stored in my working directory as 'snakes dot json'. To load the JSON into my Python environment, I would first import the package json and then open a connection to the file and use the function json dot load to load the JSON.
If I then check the datatype of json_data by executing type(json_data), I see that Python cleverly imported the JSON as a dictionary!
9. Exploring JSONs in Python
To print the key-value pairs to the console, I can then iterate over the key-value pairs using a for loop. Now it's your turn to test your JSON skills
10. Let's practice!
so get coding!