Apidog

All-in-one Collaborative API Development Platform

API Design

API Documentation

API Debugging

API Mock

API Automated Testing

Sign up for free
Home / Tutorials / Explanation: Extracting Values from JSON Response to Variables in Postman

Explanation: Extracting Values from JSON Response to Variables in Postman

This article explains how to effortlessly extract specific values from JSON responses in Postman. Learn to use test scripts to transform raw data into usable variables.

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.

💡
Apidog can significantly improve the efficiency of API development as an API management tool that can fully support API design, documentation, testing, and mock servers. By using Apidog's "variable extraction" function, you can extract values ​​from a response using the JSONPath and save them as variables intuitively.

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.

What is Postman (A Tutorial for Beginners)
What is Postman? This article will provide a comprehensive introduction to Postman, so let’s get started.
Postman

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.

postman set test code
// 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.

Check Variable Added

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.

button

Step 1: In Apidog, switch to the "Post-processing" tab and add an "Extracted Variable".

apidog extract variable post processors

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.

jsonpath extraction tool apidog

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.

apidog new post processor effective

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.

Join Apidog's Newsletter

Subscribe to stay updated and receive the latest viewpoints anytime.