pythonstockalpha-vantage

How do I get just the current price with Alpha Vantage API


I've been using the python Alpha Vantage API to get data about Bitcoin but now I need to get just the price. My code gives me this output,

{"Realtime Currency Exchange Rate": {"1. From_Currency Code": "BTC", "2. From_Currency Name": "Bitcoin", "3. To_Currency Code": "USD", "4. To_Currency Name": "United States Dollar", "5. Exchange Rate": "56057.81000000", "6. Last Refreshed": "2021-02-20 23:55:01", "7. Time Zone": "UTC", "8. Bid Price": "56057.50000000", "9. Ask Price": "56057.81000000"}}

and I need to get this to just 5.

"56057.81000000"


Solution

  • You need to access the 5. Exchange_Rate key inside Realtime Currency Exchange Rate key, i.e.:

    j_obj = {"Realtime Currency Exchange Rate": {"1. From_Currency Code": "BTC", "2. From_Currency Name": "Bitcoin", "3. To_Currency Code": "USD", "4. To_Currency Name": "United States Dollar", "5. Exchange Rate": "56057.81000000", "6. Last Refreshed": "2021-02-20 23:55:01", "7. Time Zone": "UTC", "8. Bid Price": "56057.50000000", "9. Ask Price": "56057.81000000"}}
    
    exchange_rate = j_obj["Realtime Currency Exchange Rate"]["5. Exchange Rate"]
    print(exchange_rate)
    # 56057.81000000
    

    Demo