Objective

Consume a JSON API in Python.

Distributions

This will work on any Linux distribution.

Requirements

A working Linux install with Python.

Difficulty

Easy

Conventions

  • # - requires given command to be executed with root privileges either directly as a root user or by use of sudo command
  • $ - given command to be executed as a regular non-privileged user

Introduction

One 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 File

Before 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 json
You'll need urllib3. It lets you access a URL with Python.
import urllib3

Open The URL

While 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 https://api.opendota.com/api/heroes Heroes data.

Start by creating a PoolManager() object using urllib3. It's that object that you can use to make requests to a website.
http = urllib3.PoolManager()
Make a GET request using the http object that you just created to the DoTA API.
heroes = http.request('GET', 'https://api.opendota.com/api/heroes')

Parse The JSON

Try printing out the heroes variable 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 heroes.data
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 Data

You 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.
for hero in heroes:
    print(heroes['localized_name'])
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.

Closing Thoughts

The 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.

Exercise

Pick 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.