API to Database?

yannick picture yannick · Oct 16, 2016 · Viewed 23.3k times · Source

Please presume that I do not know anything about any of the things I will be mentioning because I really do not.


Most OpenData sites have the possibility of exporting the presented file either in for example .csv or .json formats (Example). They also always have an API tab (Example API).

I presume using the API would mean that if the data is updated you would receive the change whereas exporting it as .csv would mean the content will not be changed anymore.

My questions is: how does one use this API code to display the same table one would get when exporting a .csv file.

Would you use a database to extract this information? What kind of database and how do you link the API to the database?

Answer

will-yama picture will-yama · Oct 18, 2016

I presume using the API would mean that if the data is updated you would receive the change whereas exporting it as .csv would mean the content will not be changed anymore.

You are correct in the sense that, if you download the csv to your computer, that csv file won't be updated any more.
An API is something you would call - in this case, you can call the API, saying "Hey, do you have the latest data on xxx?", and you will be given back the latest information about what you have asked. This does not mean though, that this site will notify you when there's a new update - you will have to keep calling the API (every hour, every day etc) to see if there are any changes.

My questions is: how does one use this API code to display the same table one would get when exporting a .csv file.

You would:

  1. Call the API from a server code, or a cloud service
  2. Let the server code or cloud service decipher (or "Parse") the response
  3. Use the deciphered response to create a table made out of HTML, or to place it into a database

Would you use a database to extract this information? What kind of database and how do you link the API to the database?

You wouldn't necessarily need a database to extract information, although a database would be nice to place the final data inside.
You would first need some sort of way to "call the REST API". There are many ways to do this - using Shell Script, using Python, using Excel VBA etc.
I understand this is hard to visualize, so here is an example of step 1, where you can retrieve information.
Try placing in the below URL (taken from the site you showed us) in your address bar of your Chrome browser, and hit enter http://opendata.brussels.be/api/records/1.0/search/?dataset=associations-clubs-sportifs

See how it gives back a lot of text with many brackets and commas? You've basically asked the site to give you some data, and this is the response they gave back (different browsers work differently - IE asks you to download the response as a .json file). You've basically called an API.

To see this data more cleanly, open your developer tools of your Chrome browser, and enter the following JavaScript code

var url = 'http://opendata.brussels.be/api/records/1.0/search/?dataset=associations-clubs-sportifs';

var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onload = function() {
    if (xhr.status === 200) {
        // success
        console.log(JSON.parse(xhr.responseText));
    } else {
        // error
        console.log(JSON.parse(xhr.responseText));
    }
};
xhr.send();

When you hit enter, a response will come back, stating "Object". If you click through the arrows, you can see this is a cleaner version of the data we just saw - more human readable.

enter image description here

In this case, I used JavaScript to retrieve the data, but you can use whatever code you want. You could proceed to use JavaScript to decipher the data, manipulate it, and push it into a database.

kintone is an online cloud database where you can customize it to run JavaScript codes, and have it store the data in their database, so you'll have the data stored online like in the below image. This is just one example of a database you can use.

enter image description here

There are other cloud services which allow you to connect API end points of different services with each other, like IFTTT and Zapier, but I'm not sure if they connect with open data.