Teaching high school students Computer Science (part 2)

How I taught students how to use an API and interact with JSON objects

Posted on 2017-05-30

Abstract

This was the second of the two capstone projects. They were tasked with using OpenWeatherMap's API to download weather data for a specific location and print out relevant information to the user. Because they had learned how to use Python dictionaries earlier in the course, they were well-equipped to learn how to interact with JSON objects. All they had to do was find out how to obtain one and read through it.

The Assignment

Students were asked to first obtain API keys from OpenWeatherMap and store them in a separate file that they could potentially add to future projects' .gitignore file when they learn how to use Git. This would give them experience with writing projects with multiple files, the importance of keeping passwords and keys private in their projects, as well as how they can leverage APIs.

Solution

My instructions stressed the importance of learning how to ask the right questions and where to look for help. I told students that in order to GET the JSON, they needed to use the Requests library but left it up to them to find out the right method to call (it was requests.get()). Some students were still confused and I suggested to the ones that asked questions that they Google "GET requests" and documentation on how to use requests to get the JSON. They understood the hint and found documentation in the first link they searched.

I proceeded to let the rest of the class know about the insightful questions some students asked and how they were able to understand GET requests and how to leverage requests to perform one. Once everyone was on the same page as to how to obtain the JSON, I walked them through how understand which parts of the object were most relevant to them. I used Postman to demonstrate performing a GET request on OpenWeatherMap's API and showed them how to load the JSON response using json.loads(insert_json_string_here) and interact with it in the Python REPL. After some exploring, some students were able to understand the general structure of the JSON object and how to obtain the weather for a given day.

Final Thoughts

It was a difficult project for the students but they ultimately found it fascinating that they could grab data from external sources to write their own applications. Most of the students didn't finish on their own and some students stayed after class to work through the whole project on their own. One student in particular was able to solve the entire project after explaining how he might understand the JSON response. I told them ahead of time that the projects would be difficult, but that they would have a good grasp of what to work on in their free time.