How to Extract a Single Value from JSON Response Using API Call in Python?

In this Python article, let us look at how to use Python to obtain single data or single values from JSON. To complete this task, we will use Python’s request module, which allows users to send HTTP requests and get responses in the form of JSON.

What is JSON?

JSON is an abbreviation for JavaScript Object Notation. It means that data is stored and transferred using a script (executable) file comprised of text in a computer language. Python has a built-in library named json that handles JSON. In order to use this feature, we must first load the json package into our Python script. The text in JSON is represented by a quoted-string, which contains the value in the key-value mapping within flower braces { }.

Example:

{
    "data": [
        {
            "ID": "1",
            "NAME": "Dhoni",
            "SALARY": "100000"
        },
        {
            "ID": "2",
            "NAME": “Virat”,
            "SALARY": "90000"
        }
     ]
}
Get the API Key from the URL:

Extracting a Single Value from JSON Response Using API Call in Python

Approach:

  • Import parse from urllib module using the import keyword
  • Import requests module using the import keyword
  • Declare the base URL using the API key variable.
  • Below is the base URL whereas ‘0f215802f0c83392e64ee40d’ is api key
  • Give some random string(here currency) as user input using the input() function and store it in a variable.
  • Give the other random string as user input using the input() function and store it in another variable.
  • Add both the currencies separated by the ‘/’ symbol to the base URL path to fetch the results.
  • Add the above path to the base URL and create the final URL.
  • Send an API request to the server and retrieve the data.
  • Get the conversion rate from the JSON data by accessing the key conversion_rate and store it in a variable
  • Print the Conversion Rate.

Below is the implementation:

# Import parse from urllib module using the import keyword 
import urllib.parse
# Import requests module using the import keyword 
import requests
# Declare the base url using the API key variable.
# Below is the base url whereas '0f215802f0c83392e64ee40d' is api key
baseUrl= "https://v6.exchangerate-api.com/v6/0f215802f0c83392e64ee40d/pair/"
# Give some random string(here currency) as user input using the input() function
# and store it in a variable.
print("Enter Some Random First Currency Value say (USD,INR) =")
firstcurrency=input()
# Give the other random string as user input using the input() function
# and store it in another variable.
print("Enter Some Random Second Currency Value say (USD,INR) =")
secondcurrency=input()
# Add both the currencies separated by '/' symbol to the base url path to fetch the results.
value = firstcurrency+"/"+secondcurrency
# Add the above path to the base url and create the final url
final_url = baseUrl+value
# Send an API request to the server and retrieve the data.
json_data = requests.get(final_url).json()
# Get the conversion rate from the json data by accessing the key conversion_rate and store it in a variable
resultValue = json_data['conversion_rate']
# Print the Conversion Rate
print("1 "+firstcurrency+" = ",resultValue,secondcurrency)
print("Conversion rate from "+firstcurrency+" to "+secondcurrency+" = ",resultValue)

Output:

Enter Some Random First Currency Value say (USD,INR) =
EUR
Enter Some Random Second Currency Value say (USD,INR) =
INR
1 EUR = 83.8568 INR
Conversion rate from EUR to INR = 83.8568