When testing APIs in Postman, it is often necessary to efficiently extract specific values from the response and store them in variables in order to streamline the testing process. So, how can you extract values from a JSON response to variables in Postman? In this guide, we will show you how to extract values from a JSON response to variables using Postman's test scripts.
Get Apidog today for free by clicking the button below! 👇
Postman Overview
Postman is a popular tool for API (Application Programming Interface) development and testing. With Postman, developers can create HTTP requests and send them to API endpoints to test the API functionality and inspect request and response data.
Why Extract Values from JSON Response to Variables in Postman
Usage in Test Scripts
Extracted values can be used for data validation in test scripts or passed on to subsequent requests.
Usage in Subsequent Requests
Extracting identifiers (IDs, etc.) from responses allows for straightforward referencing in subsequent requests.
Handling Dynamic Data
Capture dynamically changing data (like authentication tokens) from responses for use in Postman's subsequent requests.
Extracting JSON Response Values to Variables in Postman Test Scripts
Here's how you can extract values from a JSON response to variables using Postman's test scripts:
Step 1: Send a request in Postman and determine which value from the response you want to extract to a variable.
Example JSON response:
{
"code": 0,
"data": {
"id": 1,
"name": "Pet Name",
"status": "available"
}
}
For example purposes, let's save the status
field as an environment variable.
Step 2: Switch to the "Tests" tab in the request view in Postman, and input the following script.
// Define responseData object with JSON response data
let responseData = pm.response.json();
// Extract the value of the 'status' field under data object of the response data and save it as a variable 'PetStatus'
pm.environment.set("PetStatus", responseData.data.status);
Step 3: Click the "Send" button to execute the request. The test script you inputted will run automatically. You can then check if the defined variable has been added to the environment variables list by clicking on the Environment selector in the upper right corner.
Using Apidog for Convenient Variable Extraction
While the method mentioned above works well for extracting response values to variables in Postman, a more convenient approach for extracting JSON response values to variables is by using Apidog, a powerful API management tool.
Step 1: In Apidog, switch to the "Post-processing" tab and add an "Extracted Variable".
Step 2: Define the variable name and specify the value you want to extract from the JSON response using JSONPath.
Note: If you're unfamiliar with JSONPath syntax, you can click on the "Expand" icon in the input box to open a JSONPath extraction tool.
Step 3: Click the "Send" button to execute the request. The post-processing step will run automatically. You can verify that the defined variable has been added to the environment variables list by clicking on the environment settings in the upper right corner.
Conclusion
In this guide, we've explored how to extract values from JSON responses to variables in Postman and Apidog to enhance the efficiency of API testing processes. Leveraging these features in Postman and Apidog allows for more streamlined testing, data-driven testing, and improved API development workflows.