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)
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'}}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With