Data Extraction from APIs with Python – Currency Exchange

There are several popular platforms that give developers access to their “web services”, aka “APIs” (Application Programming Interface). So using APIs is the official way for data extraction and doing other stuff allowed by such applications. You can even benefit from some APIs to build other applications. REST APIs usually generate output in JSON or XML format because most of programming languages can handle these formats easily. In fact, JSON (JavaScript Object Notation) is very similar to data types in programming languages; for example, it is very similar to Python dictionaries. If a REST API allows you to get the data you want to retrieve, then you do not need regular web scraping.

Some APIs require authentication (API Key or Client ID and Client Secret, similar to a username and password, so to speak) to control their usage, and some do not. We will explain this later in multiple APIs. For the purpose of clarifying the basics, we will start with a very simple currency rate conversion API that does not require any authentication.

So this tutorial explains how to use Python to extract data from Fixer.io which is -according to its official website- “a free JSON API for current and historical foreign exchange rates published by the European Central Bank.”

API URL

Just as most REST APIs that return JSON, Fixer API gives you a URL with a “query string” with one “parameter” or more. Here is an example URL for finding the rates of USD and GBP in EUR: http://api.fixer.io/latest?symbols=USD,GBP

If you click this URL, you will get this result:

 

As you can see in this example, in Python terms, it is like a dictionary that includes a smaller dictionary. This can get complicated in other APIs like having also lists and multiple dictionaries – again in Python terms.

 

Note: To preview the JSON output in a readable format, you can use Firefox. By default, Firefox has a built-in JSON viewer that shows JSON in a nice format once you open the URL. In the Firefox JSON viewer, you can also click the tab called “Raw Data” and then the sub-tab “Pretty Print” to see JSON in a view similar to how it is displayed above. Chrome can have the same with extensions. For more complicated tasks, you can download Postman free app, but for now the Firefox built-in JSON is just enough. So keep it simple for now and let’s continue.

 

Reading and Parsing the API Output with Python

 

1- To handle the API output, you need to import two Python libraries:

requests (or urllib2 or the like) to connect to the URL.

json to parse the JSON output and extract the data you need.

 

 

2- Connect to the URL as if you are opening it in browser – figuratively 😉

 

If you print(response) you will get <Response [200]> which means you are successfully connected.

 

3- Read the output:

 

Note: At this point, you can try to find out the type of “data” using type(data) and you will get str which means “string”. Actually, if you print(data), you will see how it is enclosed with quotes.

 

4- Parse JSON – convert the string to JSON:

 

Note: You can now try to find out the type of “parsed” using type(parsed) and you will get dict which means “dictionary”. If you print(parsed) you will see the quotes is now removed. Be careful, it is “loads” not “load”.

 

You can even have a “pretty print” to make it more readable for you. This step is not required though.

The output will look like this:

 

So now, you can deal with it as a regular dictionary.

 

5- Extract data:

 

If you want to extract “date”, just type:

 

Now you can extract the rate of GBP by accessing the main dictionary “parsed”, then sub-dictionary “rates” and finally the key “GBP” to get its value. Be careful, do not forget quotes.

 

You can extract the rate of USD in the same way:

Then try to print them:

 

So here is your whole code:

 

Get Rates of Other Currencies

 

You can use this URL to get rate of all the available currencies while the base is USA. Feel free to open it in your browser to see how it looks like. http://api.fixer.io/latest?base=USD

 

As you can see, it is a dictionary including the rates dictionary. So let’s extract the rates dictionary:

 

Try to print it if you like to see how it looks like:

 

Here is how it looks like:

 

So now to get the currencies and their rates, you need to iterate the keys and values of this dictionary using items() like this:

 

Note: In Python 3, you should use items() while in Python 2, you should use iteritems()

 

You can do it another way:

 

 

Our full code will be as follows:

 

Changing API URL Parameters

 

Actually, you can add the parameters in the URL you add to your code. Still, in some cases it helps to do this programmatically, which can be easily done using standard Python concatenation.

 

For example, instead of having USD as the only base, you can have a list of “base” currencies to iterate them like this:

 

So that is it for this initial API tutorial, and we going to publish more tutorials on the topic soon.

 

You can learn more about APIs and Python by joining this course for free: 30 Days of Python | Unlock Your Python Potential by Justin Mitchel.

 

 

Rating: 5.0. From 6 votes.
Please wait...

2 Replies to “Data Extraction from APIs with Python – Currency Exchange”

  1. Hi, thanks for explicit tutorial, I’m wondering if I have a JSON Array like:
    [
    {
    “id”: “ethereum”,
    “symbol”: “ETH”
    }
    ]
    how then I could print out “id” and “symbol”?

    No votes yet.
    Please wait...
    1. Hi Domas!

      If you have one item in the list (array), the following should work. If you rather have more than one, you will need a for loop.

      import json

      array = ”'[
      {
      “id”: “ethereum”,
      “symbol”: “ETH”
      }
      ]”’

      parsed_array = json.loads(array)

      print(parsed_array)

      print(parsed_array[0][‘id’])
      print(parsed_array[0][‘symbol’])

      No votes yet.
      Please wait...

Leave a Reply