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:

USD To INR

INR TO EUR

Python program to extract a single value from JSON response

We will discuss how Python can be used to extract a value from a JSON response using API and JSON files.

Similar Reads

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....

Method-2:

...

Extract values from a JSON File

Using the jsonpath-ng library to extract values from the JSON response. jsonpath-ng is a fork of the jsonpath library and allows for more powerful querying of JSON data using expressions similar to those used in XPath....

Contact Us