Search code examples
pythonjsonpandasurllib3

HTTPResponse Error in urllib python


I'm trying to use pandas to read coinmarketcap API . https://stackoverflow.com/a/40589031/7159086 I used the above link to form a basic query using json_normalize but I am getting the below error -

'HTTPResponse' object does not support indexing .

My Query :-

from urllib.request import urlopen
import pandas as pd

pd.io.json.json_normalize(urlopen('https://api.coinmarketcap.com/v2/ticker/?limit=10'))

Output :-

TypeError: 'HTTPResponse' object does not support indexing

I am running this code in Jupyter Notebook with pandas version 0.20.3 I saw this post also - Retreiving data from a website

But I was not able to solve my issue still . Please tell how to approach this problem . Further , I want name field as the index column which I am not able to get due to nested json.


Solution

  • urlopen produces an HttpResponse object, not a json string. You need to call its read method to get the json.

    Change

    pd.io.json.json_normalize(urlopen('https://api.coinmarketcap.com/v2/ticker/?limit=10'))
    

    to

    pd.io.json.json_normalize(urlopen('https://api.coinmarketcap.com/v2/ticker/?limit=10').read())
    

    Update

    json_normalize expects deserialised json, so you need to load the json before passing it to json_normalize

    resp = urlopen('https://api.coinmarketcap.com/v2/ticker/?limit=10')
    data = json.load(resp)
    pd.io.json.json_normalize(data)