Data modeling and APIs

Learn this with friends

ComputingIntro to computer science - Python4:174 check-ins

About this video

When a dictionary has too many fields, we can organize it by nesting smaller data structures inside it, a process called data modeling. The video then explains how front ends and back ends communicate through APIs, which usually send nested, dictionary-like data in a format called JSON.

You'll be able to

  • explain what data modeling is and why grouping related fields helps.
  • describe the roles of a front end, a back end and an API.
  • recognise JSON as the dictionary-like format most web APIs use.

Key words

data modeling
Designing a meaningful structure for data so it is easier to understand and use.
nested data
Data structures placed inside other data structures, like a dictionary inside a dictionary.
back end
The main program logic that gets data, processes it and does calculations.
front end
The part users interact with, which requests data and displays it on screen.
API
Application Programming Interface: a set of functions a back end makes available over the internet.
JSON
A dictionary-like data format most web APIs use to send data.
Transcript

- [Instructor] We have a bunch of data and we stored it in a dictionary, but now we have 20 fields, a hundred fields, and all of this data is getting kind of hard to read. I have to scan through all of these fields just to find the piece of data that I need. Now remember when this same thing happened with our code, when our programs got to be too long at the top level, we started breaking them down into functions.

We can apply that same idea to our data. When our data gets to be too big at the top level, we can break it down into smaller data structures, which we can then combine together. By organizing our data into meaningful chunks, we make it easier to understand for all of the programmers out there trying to use it.

Let's take a look at a slightly smaller example. Here we have data for a student record. Right now, all of the fields are stored at the top level, but there are relationships in this data that aren't being clearly represented like all of these fields are for an address and all of these fields are for different classes.

Wouldn't it make more sense if all of the data for a single class was grouped into its own dictionary? We call this process data modeling. We design a meaningful structure for our data that makes it easier to understand and use. So I might group all of these fields for an address into a smaller address dictionary, and then I might group all of the fields related to the student's guardian into its own guardian dictionary and so on and so on.

Adding structure to my data by nesting smaller data structures inside the larger dictionary. You can think of this just like adding structure to a document. You take all of those words and then you group them into sections, and within those sections you have subheadings and maybe those subheadings have other subheadings until finally you have it organized so someone can come in and use that structure to easily find the information they're looking for.

Okay, but who's making data structures that are this complicated in the first place? It turns out pretty much the entire internet. The websites and apps that you use have both a backend and a front end component. The backend is all of the main program logic. It might retrieve some information from a database, process or filter it in some way and then perform some calculations on it.

The front end requests specific data from the backend, and then it displays it on the screen in an appealing and useful way. In most cases, the end user only interacts with the front end, but what's happening in the middle? This layer is called an API or Application Programming Interface.

An API is essentially a special set of functions that a backend program has made available over the internet. For example, when you do navigation in a Maps app, the front end needs to first request all of that directions data from the backend, so maybe it calls a specific API endpoint called get_directions, and then the front end passes in some information about what directions the user is looking for.

The backend then processes that request. It likely retrieves some data from a database and then does some calculations with it. Returning the final result back through the API to the front end, which the front end then displays as this nice little map overlay.

The raw data that came through that API endpoint however, was formatted just like a dictionary. This dictionary format is called JSON, and it's the format most APIs on the web use to transmit data. In most of the programs we write, we won't be the ones creating the data, we'll just be using it.

So we can only hope that whoever did create it modeled the data well. Our job as a programmer then is to learn how to access the fields in a nested data structure and then to do something interesting with it.

Video © Khan Academy, used under CC BY-NC-SA. Watch on Khan Academy. Puzzicle is not affiliated with Khan Academy.