Skip to content

Instantly share code, notes, and snippets.

@rruntsch
Created November 21, 2022 15:32
Show Gist options
  • Save rruntsch/fbc05a837dc9098219ff4a4e98a5f3c7 to your computer and use it in GitHub Desktop.
Save rruntsch/fbc05a837dc9098219ff4a4e98a5f3c7 to your computer and use it in GitHub Desktop.
Example of calling WHO GHO OData API with Python
#
# Name: c_who_mortality.py
# Date: April 17, 2022
# Author: Randy Runtsch
#
# Description:
#
# Obtain WHO Global Observatory (GO) records in JSON format with the GHO Odata API for IndicatorCode: NCDMORT3070,
# IndicatorName: Probability (%) of dying between age 30 and exact age 70 from any of
# cardiovascular disease, cancer, diabetes, or chronic respiratory disease.
#
# Convert the JSON records to a Python list of dictionaries. Then, write select fields
# for records with a SpatialDimType of "COUNTRY" to the specified CSV output file.
#
import requests
import json
import csv
class c_who_mortality_data:
def __init__(self, out_file_nm):
# Set the file name variable and create the parameters for the API request.
self.out_file_nm = out_file_nm
headers = {'Content-type': 'application/json'}
# Get data in JSON format and then write it to a CSV file.
data_list = self.get_data(headers)
self.write_data_to_csv(data_list)
def get_data(self, headers):
# Post the data request to the BLS API. Return the resulting JSON structure.
post = requests.post('https://ghoapi.azureedge.net/api/NCDMORT3070', headers = headers)
data = json.loads(post.text)
data_list = data['value']
return data_list
def write_data_to_csv(self, data_list):
# Convert the data from a list of dictionaris to CSV records. Write
# each record to the specified output file.
# Open the output file. Then, set up the field names for the CSV records and set up the CSV writer.
with open(self.out_file_nm, mode = 'w', newline = '') as data_file:
data_writer = csv.writer(data_file, delimiter = ',', quotechar = '"', quoting = csv.QUOTE_ALL)
# Write CSV file header.
header = ['country_code', 'year', 'sex', 'value']
data_writer.writerow(header)
# Write each record to the output file.
for record in data_list:
spatial_dim_type = record['SpatialDimType']
# Write COUNTRY records only. Exclude records for REGIONS, etc.
# Get the fields of interest.
if spatial_dim_type == 'COUNTRY':
country_code = record['SpatialDim']
year = record['TimeDim']
sex_code = record['Dim1']
sex = self.get_sex(sex_code)
value = record['NumericValue']
#Write the CSV record to the output file.
record = [country_code, year, sex, value]
data_writer.writerow(record)
data_file.close()
def get_sex(sef, sex_code):
# Convert the sex code to a sex name.
sex = ''
if sex_code == 'FMLE':
sex = 'Female'
elif sex_code == 'MLE':
sex = 'Male'
elif sex_code == 'BTSX':
sex = 'Both Sexes'
else:
sex = 'Unknown'
return sex
#
# Name: get_who_mortality_data.py
# Date: November, 2022
# Author: Randy Runtsch
#
# Description:
#
# Use the c_who_mortality_data class to retrieve mortality
# data for select diseases using the WHO GHO OData API.
#
from c_who_mortality_data import c_who_mortality_data
c_who_mortality_data('c:/who_data/mortality.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment