We will discuss how Python can be used to extract a value from a JSON response using API and JSON files.
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 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:
Method-2:
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.
# install jsonpath-ng library pip install jsonpath-ng
Here’s an example of how you could use jsonpath-ng to extract the conversion rate from the JSON response in the first example:
Python3
# import required libraries import urllib.parse import requests from jsonpath_ng import jsonpath, parse # setting the base URL value # ask user to enter currency values First = input ( "Enter First Currency Value: " ) Second = input ( "Enter Second Currency Value: " ) # combine base URL with final URL including both currencies result = First + "/" + Second final_url = baseUrl + result # send API call and retrieve JSON data json_data = requests.get(final_url).json() # set up jsonpath expression to select conversion rate jsonpath_expr = parse( '$.conversion_rate' ) # use jsonpath expression to extract conversion rate result = jsonpath_expr.find(json_data)[ 0 ].value print ( "Conversion rate from " + First + " to " + Second + " = " , result) |
Using jsonpath-ng can make it easier to extract specific values from complex JSON structures without having to manually navigate through the data.
Extract values from a JSON File
To create a JSON file open a text editor either notepad or VSCode then copy the above code and save the code with the .json extension.
{“criteria”: [
{“locationParam”: “[ALL:03232434]” },
{“variableParam”: “[00060, 00065]” }
]}
Fetch all Values from JSON file
Import JSON from the modules. Open the JSON file in read-only mode and load the JSON data into a variable using the Python load() function. Print the variable where the JSON data is loaded. The load function stores the JSON data as a Python dictionary of key-value pairs.
Python3
import json with open ( 'exam.json' , 'r' ) as json_File : sample_load_file = json.load(json_File) print (sample_load_file) |
Output:
Fetch specific Values from the JSON file
Import JSON from the modules. Open the JSON file in read-only mode using the Python with() function. Load the JSON data into a variable using the Python load() function. Now, get the value of keys in a variable. Now convert the value of the dictionary into a list and slice the string using the split function.
Python3
import json with open ( 'exam.json' , 'r' ) as json_File: sample_load_file = json.load(json_File) # getting hold of all values inside # the dictionary test = sample_load_file[ 'criteria' ] # getting hold of the values of # variableParam test1 = test[ 1 ].values() test2 = list (test1)[ 0 ] test3 = test2[ 1 : - 1 ].split( "," ) print (test3[ 1 ]) |
Output: