I am struggling with data from the International Monetary Fund, which is in JSON format. After inspecting some of the posts, I couldn't figure out how to do it.
What I tried
import requests
import pandas as pd
import json
# These are the variables I want to have as columns, plus setting a time index
var = ['NGDP_XDC', 'NCP_XDC', 'NCGG_XDC', 'NFI_XDC', 'NINV_XDC', 'NX_XDC',
'NM_XDC', 'NSDGDP_XDC', 'NGDP_R_K_IX', 'NGDP_D_IX']
# URL for the IMF JSON Restful Web Service,
# IFS database
base = 'http://dataservices.imf.org/REST/SDMX_JSON.svc/CompactData/IFS/'
period = 'A'
country = 'MX'
var = 'NGDP_XDC+NCP_XDC+NCGG_XDC+NFI_XDC+NINV_XDC+NX_XDC+NM_XDC+NSDGDP_XDC+NGDP_R_K_IX+NGDP_D_IX'
time = '?startPeriod=1970&endPeriod=2019'
# Get data from the above URL using the requests package
url = base + period + '.' + country + '.' + var + '.' + time
response = requests.get(url)
dictr = response.json()
... so far so good... However, this is the step I am struggling with
flat = dictr['CompactData']['DataSet']['Series']
temp = pd.json_normalize(flat)
temp = temp.drop(columns=['@FREQ', '@REF_AREA', '@UNIT_MULT', '@BASE_YEAR'])
I was expecting a flat-file that I could pivot to my will. However, this is what I get
@INDICATOR @TIME_FORMAT Obs
0 NINV_XDC P1Y [{'@TIME_PERIOD': '1970', '@OBS_VALUE': '37.21...
1 NX_XDC P1Y [{'@TIME_PERIOD': '1970', '@OBS_VALUE':
Which I have no clue how to transform it into
year variable1 ... variableN
1970 10 ... 45
1980 20 ... 12
.
.
.
2019 15 ... 10
Maybe this will nudge you in the right direction.
The value of
['CompactData']['DataSet']['Series']
is adict
that contains a list of dicts as its value that you're after.So you have to flatten this:
Putting it all together:
Output: