Extract value from the JSON response using the API
Initially, use the API Key variable to declare the base URL. Where the first currency needs to be converted with the second, ask the user to enter a currency name and save it in a variable. The base URL is combined with the final URL, which includes both currencies, to fetch the result. An API call is then sent. The data is obtained by accessing the JSON Data’s “conversion rate” key, and the resulting conversion rate is then printed.
API Key is available at: https://exchangeratesapi.io/documentation/
Python3
# importing required module import urllib.parse import requests # setting the base URL value baseUrl = "https://v6.exchangerate-api.com/v6/0f215802f0c83392e64ee40d/pair/" First = input ( "Enter First Currency Value" ) Second = input ( "Enter Second Currency Value" ) result = First + "/" + Second final_url = baseUrl + result # retrieving data from JSON Data json_data = requests.get(final_url).json() Final_result = json_data[ 'conversion_rate' ] print ( "Conversion rate from " + First + " to " + Second + " = " , Final_result) |
Output: