We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Fetching JSON

So far, our JSON has been sitting there waiting for us. But in many cases, you'll need to go fetch it from an API first. If an API serves you JSON over HTTP, that means you'll:

  1. Make a request
  2. Read the response body
  3. Parse the JSON

Python's built-in urllib.request module can make simple HTTP requests.

import urllib.request

url = "https://api.example.com/stocks"

with urllib.request.urlopen(url) as response:
    raw_data = response.read()
    data = json.loads(raw_data)

Assignment

Complete the fetch_device_status function. It accepts a device_id string and fetches its status (a dictionary) from the SnackStack API.

  1. f"https://api.snackstack.dev/devices/{device_id}/status"