VOOZH about

URL: https://www.geeksforgeeks.org/python/python-program-to-extract-a-single-value-from-json-response/

⇱ Python program to extract a single value from JSON response - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python program to extract a single value from JSON response

Last Updated : 23 Jul, 2025

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/

Output:

👁 Image
USD To INR
👁 Image
INR TO EUR

Extract Values from the JSON Response Using jsonpath-ng Library

The 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:

👁 Image
Output

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 in Python

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.

Output:

👁 Image

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.

Output:

👁 Image
Comment