Not receiving response

Sankalpkotewar
I did the following basic steps to get JSON response:

(And yes I replaced xxx and yyy with my apikey and access_token)

response = requests.get(
"https://api.kite.trade/instruments/NSE/SBIN?api_key=xxx&access_token=yyy")
data = json.dumps(response).encode('utf-8')
data = json.loads(data)
new_value = data["data"]["ohlc"]["close"]

but I am unable to get data, and instead I am getting this error:


File "trade.py", line 29, in arr_val
data = json.dumps(d).encode('utf-8')
File "C:\Python27\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Python27\lib\json\encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Python27\lib\json\encoder.py", line 264, in iterencode
return _iterencode(o, 0)
File "C:\Python27\lib\json\encoder.py", line 178, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: is not JSON serializable

Tell me how to resolve this error, all I want is to fetch olhc close value from the response I am getting.

Thanks.
Tagged:
  • sujith
    sujith edited March 2017
    @Sankalpkotewar,
    Instead of
    data = json.dumps(response).encode('utf-8')
    use
    data = json.dumps(response.content)
    response is an object you can get body in content.
  • Sankalpkotewar
    @sujith
    I am currently using Python 2.7.13
    and after doing

    data = json.dumps(response.content)

    It is showing error

    AttributeError: 'dict' object has no attribute 'content'
Sign In or Register to comment.