API to Database?
Asked Answered
O

3

13

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?

Oxime answered 15/10, 2016 at 22:15 Comment(0)
L
14

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.

Larson answered 18/10, 2016 at 8:19 Comment(0)
C
0

The page you linked to shows that the API returns values as a JSON object. To access the data you can just send an appropriate http request and the response will be the requested data as a JSON. You can send requests like that over your browser if you want to.

Most languages allow JSON objects to be manipulated pro grammatically if you need to do work on the data.

Colonialism answered 15/10, 2016 at 23:6 Comment(1)
would you perhaps know a user-friendly tool with which I would be able to use to extract the API values into a database/table without coding? Simply inserting the API into a text box and clicking on a magic button which creates a table?Oxime
B
0

Restful APIs publish model is "request and publish". Wen you request data via an API endpoint, you would receive response strings in JSON objects, CSV tables or XML.

The publisher, in this case Opendata.brussel.be would update their database on regular basis and publish the results via an API endpoint.

If you want to download the table as a relational data table in a CSV file, you'd need to parse the JSON objects into relational tables. This can be tricky since each JSON response string can vary in their paths.

There're several ways to do it. You can either write scripts to flatten the JSON objects or use a tool to parse and flatten the objects for you.

I use a tool called Acho to turn API endpoints into CSV files. It would parse almost all API endpoints through the parameters and even configure for multiple requests, such as iterative and recursive requests.

Acho API parser

Beverie answered 11/4, 2022 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.