Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am i getting Object of type method is not JSON serializable when making this api call

Tags:

python

rest

I am trying to interact with the coinbase api and keep getting "TypeError: Object of type method is not JSON serializable" when trying to print out this json data, I know the get request is correct as it returns a 200 when I remove the json.dumps().

    import requests
    import json


    response = requests.get('https://api.coinbase.com/v2/prices/BTC-USD/buy')
    data = json.dumps(response.json)
    print(data)
like image 552
Billy Avatar asked Sep 18 '25 02:09

Billy


2 Answers

Try this:

response = requests.get('https://api.coinbase.com/v2/prices/BTC-USD/buy')
data = json.loads(response.text)
print(data)  # {'data': {'base': 'BTC', 'currency': 'USD', 'amount': '40935.10'}}
like image 118
CJR Avatar answered Sep 19 '25 16:09

CJR


Your return is:

{'data': {'base': 'BTC', 'currency': 'USD', 'amount': '40880.98'}}

It has a JSON syntax error

Try this

response = requests.get('https://api.coinbase.com/v2/prices/BTC-USD/buy')
data = response.json()
dataJson = json.dumps(data['data'])
print(dataJson)
like image 37
Jinyoung So Avatar answered Sep 19 '25 16:09

Jinyoung So