Sumtinlazy
Sumtinlazy

Reputation: 347

Unable to use json.loads() due to 'expected string or buffer'

I've been stuck trying to pull a certain bit from this api response for a while.

my code:

payload = {
    'symbol':'RPX-ETH',
    'from':'100603756',
    'to':'9516619507'
}
request = requests.get('https://api.kucoin.com/v1/open/chart/history', 
params=payload)
jdata = json.loads(request)
print jdata['c']

However I keep getting this error:

TypeError: expected string or buffer

The api response only using .json() for reference:

{u'c': [0.00024, 0.000171, 0.000163, 0.000151, 0.000159, 0.000164}

Upvotes: 0

Views: 2260

Answers (2)

Rakesh
Rakesh

Reputation: 82765

You can use the request.json to access the return data as a dictionary.

Replace

jdata = json.loads(request)
print jdata['c']

With

jdata = request.json() 
print jdata['c']

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599630

request is the whole requests response object. You need to pass request.body.

However there is no need to do that at all because request.json() does it for you and returns a parsed Python data structure.

Upvotes: 3

Related Questions