CoinMarketCap API Price Notifier - Error

import requests
from datetime import datetime

currency = 'KES'

global_url = 'https://api.coinmarketcap.com/v1/global/?convert=' + currency

request = requests.get(global_url)
results = request.json()

# print(json.dumps(results, sort_keys=True, indent=4))

active_currencies = results['data']['active_cryptocurrencies']
activate_markets = results['data']['active_markets']
bitcoin_percentage = results['data']['bitcoin_percentage_of_market_cap']
last_updated = results['data']['last_updated']
global_cap = int(results['data']['quotes'][currency]['total_market_cap'])
global_volume = int(results['data']['quotes'][currency]['total_volume_24h'])

active_currencies_string = '{:,}'. format(active_currencies)
active_markets_string = '{:,}'. format(activate_markets)
global_cap_string = '{:,}'. format(global_cap)
global_volume_string = '{:,}'. format(global_volume)

last_updated_string = datetime.fromtimestamp(
    last_updated).strftime('%B %d, %Y at %I:%M%p')

print()
print('There are currently' + active_currencies_string +
      'active cryptocurrencies and ' + active_markets_string + 'activate markets.')
print('The global cap of all cryptos is ' + global_cap_string +
      'and 24h global volume is' + global_volume_string + ',')
print('Bitcon\'s total percentage of the global cap is' +
      str(bitcoin_percentage) + '%')
print()
print('This information was updated on' + last_updated_string + '.')

Python Help Error that appears after running the codes

$ python coincap_global.py
Traceback (most recent call last):
  File "C:\Users\admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\AppData\Local\Programs\Python\Python312\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\admin\desktop\capstone_project\coincap_global.py", line 10, in <module>
    results = request.json()
              ^^^^^^^^^^^^^^
  File "C:\Users\admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

admin@DESKTOP-T65F711 MINGW64 ~/desktop/capstone_project

Please read the pinned thread and format the code properly (and use the same formatting for the error message); and please try to explain a little bit about what you are trying to do and what you want to know.

You’re expecting that the request always returns JSON data, but, clearly, it doesn’t.

Try printing the value of request.text to see what it’s actually returning.

1 Like