To convert JSON to a Python Pandas DataFrame, we can use the json.loads
and json.normalize
method.
For instance, we write
from urllib2 import Request, urlopen
import json
import pandas as pd
path1 = '52.974049,-81.205203|52.974298,-81.195755'
request=Request('http://maps.googleapis.com/maps/api/elevation/json?locations='+path1+'&sensor=false')
response = urlopen(request)
elevations = response.read()
data = json.loads(elevations)
df = pd.json_normalize(data['results'])
to get some JSON data from request
.
Then we call response.read
to read the JSON response into JSON.
Next, we call json.loads
to load the JSON data into a dictionary.
And then we call Pandas json_normalize
to load the values we want into a Pandas dataframe.