top of page
Untitled

DATA DOUBLE CONFIRM

Monetary Authority of Singapore (MAS) API


This post shows an example of how to query data using the MAS API. The results returned are in bytes format and has to be transformed to json first which we will then convert to a pandas dataframe.


import pandas as pd
import urllib.request
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'headers={'User-Agent':user_agent,}

url = 'https://eservices.mas.gov.sg/api/action/datastore/search.json?resource_id=5f2b18a8-0883-4769-a635-879c63d3caac'
request=urllib.request.Request(url,None,headers) #The assembled request
response = urllib.request.urlopen(request)
data = response.read() 

import json
data_json = json.loads(data)
df = pd.json_normalize(data_json['result']['records'])
df

The jupyter notebook containing the output can be here:

https://github.com/hxchua/datadoubleconfirm/blob/master/notebooks/mas-api.ipynb. The notebook also demonstrates how to visualize the data using matplotlib and plotly.




Comments


bottom of page