ObjectiveConsume a JSON API in Python.
DistributionsThis will work on any Linux distribution.
RequirementsA working Linux install with Python.
- # - requires given command to be executed with root privileges either directly as a root user or by use of
- $ - given command to be executed as a regular non-privileged user
IntroductionOne of the main reasons that you'd like to work with JSON in Python is consuming APIs. There are hundreds of excellent public APIs out there and ready to use in your application. Even huge players on the web, like Facebook and Twitter, out out APIs for you to work with.
You can build entire applications around API data, including building web applications that aggregate, manipulate, and display that data in a convenient way.
Set Up A FileBefore you start working with APIs, you need to set up a Python file. It's much easier than working in the interpreter. Start off by importing the JSON module.
import jsonYou'll need
urllib3. It lets you access a URL with Python.
Open The URLWhile there are a ton of great APIs on the Internet, most of them require you to sign up in order to get an API key. This guide isn't about that or using a specific API. The OpenDota API provides access to a ton of data pertaining to the popular MoBA DoTA2 without the need to sign up or get a key. In case you didn't know, it is available for Linux. That's the API that this guide will use.
You can find the documentation for the whole API at
https://docs.opendota.com, but this guide will be using the
Start by creating a
urllib3. It's that object that you can use to make requests to a website.
http = urllib3.PoolManager()Make a
GETrequest using the
httpobject that you just created to the DoTA API.
heroes = http.request('GET', 'https://api.opendota.com/api/heroes')
Parse The JSONTry printing out the
heroesvariable that you just created.
print(heroes)Not what you expected? It's still a request object, and it has more data than you need. Try printing out
print(heroes.data)That looks closer, but it's still not quite there. Decode the data to UTF-8.
print(heroes.data.decode('UTF-8'))Now, that's JSON. You can parse that with the JSON module.
heroes_dict = json.loads(heroes.data.decode('UTF-8')) print(heroes_dict)
Work With The DataYou have a Python dictionary containing all of the data from the API. You can now use that data however you choose. Try iterating over it.
Your loop will print out the name of every hero in DoTA2. You can see from the dictionary that there is ton's more data, but you know how to access that.
for hero in heroes: print(heroes['localized_name'])
Closing ThoughtsThe road there wasn't all that direct, but it certainly got there. If you're doing this with API keys, it'll just change the URL structure. Everything else should remain the same throughout. You are now equipped to access API data from your Python programs.
ExercisePick and API from
https://github.com/toddmotto/public-apis, and convert it into a Python dictionary. Iterate over it, and print out the values of at least two keys.