Python Tutorial – How to make a JSON REST API Call

Intro

In this article, we will make a REST API call to OpenWeatheMap service and get the weather information in JSON data format using Python script.

Setup OpenWeatherMap Account

Before we jump into Python scripting, you need to have an account with OpenWeatherMap service. Creating an account with OpenWeatherMap service is free. Go to OpenWeatherMap website and sign up.

Get you API Key

Once you have an account with OpenWeatherMap. Go to your API Key page and grab the API Key displayed on that page.

You should also read How to use APPID so you have an idea how to make a service call to OpenWeatherMap service.

Making a REST API call using Python

Once you have the OpenWeatherMap account and have your API Key with their service, now let’s move to Python side and start scripting in Python.

Python Module

You will need the following Python module in your system. In order to make a REST API call to the OpenWeatherMap service.

requests

If you do not have requests module in your system, run a pip3 install to install it. You can check first using pip3 list to find out if you have the module installed already.

Check what Python packages you have in your system

pip3 list

Use the pip3 install <package name> to install the Python package you need for your project. In this exercise, you will need the requests Python package

pip3 install requests

or

sudo pip3 install requests

Script

Below is the complete Python script to connect to the OpenWeatherMap service, getting the weather info and just grab the temperature data.

import requests

OWM_API = "https://api.openweathermap.org/data/2.5/weather"
city_name = "london"
APPID = "<USE YOUR API KEY>"

# parameters to send along with REST API Endpoint
payload = {'q': city_name, 'APPID': APPID}

# make a requests call using GET (including parameters)
r = requests.get(OWM_API, params=payload)

# get the returns json data
json_data = r.json()

# temperature is stored in main > temp (in the form of Kelvin)
temp_in_kelvin = json_data['main']['temp']
temp_in_celcius = temp_in_kelvin - 273.15

print(f'Temp in K: {temp_in_kelvin}')
print(f'Temp in C: {temp_in_celcius}')

Notes

If you made a call to OpenWeatherMap (in this case using City Name as your passing parameter.) it will return a weather info in JSON format. The JSON data will include the details such as shown in below. You can use an online JSON viewer to view the JSON data in more friendlier way if you are not use to how to consume JSON data.  

Sample JSON data returning from OpenWeatherMap Service:

{
  "coord": {
    "lon": -122.09,
    "lat": 37.39
  },
  "weather": [
    {
      "id": 500,
      "main": "Rain",
      "description": "light rain",
      "icon": "10d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 280.44,
    "pressure": 1017,
    "humidity": 61,
    "temp_min": 279.15,
    "temp_max": 281.15
  },
  "visibility": 12874,
  "wind": {
    "speed": 8.2,
    "deg": 340,
    "gust": 11.3
  },
  "clouds": {
    "all": 1
  },
  "dt": 1519061700,
  "sys": {
    "type": 1,
    "id": 392,
    "message": 0.0027,
    "country": "US",
    "sunrise": 1519051894,
    "sunset": 1519091585
  },
  "id": 0,
  "name": "Mountain View",
  "cod": 200
}

GitHub

If you want to download the .py file, you can grab it from GitHub link below.

Sample Code

Feel free to share this post!

Scroll to Top