Book Image

Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

By : Dan Nixon
Book Image

Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

By: Dan Nixon

Overview of this book

Table of Contents (18 chapters)
Getting Started with Python and Raspberry Pi
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Parsing JSON APIs


In this section, we will be creating a simple currency converter application that will be run from the command line using the free to use Fixer.io API (http://fixer.io) to provide the exchange rates, which are updated daily (which is less frequent than some other paid for APIs, but will be good enough for our use).

This is a JSON API; an example URL is: http://api.fixer.io/latest?base=GBP&symbols=JPY,EUR

This is making a request for the exchange rates to convert British pounds to Euro and Yen and returns data in the format:

{
    "base": "GBP",
    "date": "2015-07-08",
    "rates": {
        "JPY": 186.64,
        "EUR": 1.3941
    }
}

As we will see in the next code, this data can be parsed using the json Python module, which will return the structure of the JSON tree as a nested tree of Python dictionaries.

We will start by importing the required Python modules for this script, which we will save as currency_converter.py:

import urllib2
import json
from string import...