Apidog

All-in-one Collaborative API Development Platform

API Design

API Documentation

API Debugging

API Mocking

API Automated Testing

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.

David Demir

David Demir

Updated on November 29, 2024

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.

How to Access Claude 3.7 Sonnet API and Test Using ApidogTutorials

How to Access Claude 3.7 Sonnet API and Test Using Apidog

Learn how to access Anthropic’s Claude 3.7 Sonnet API and test it with Apidog in this step-by-step tutorial. Discover setup, configuration, and best practices for exploring Claude’s coding and reasoning powers efficiently, perfect for developers seeking to leverage this advanced AI model.

Ashley Innocent

February 25, 2025

A Developer’s Guide to Testing LLM AI APIs with SSETutorials

A Developer’s Guide to Testing LLM AI APIs with SSE

Discover how Apidog's upgraded SSE debugging enhances real-time interaction with AI models. Streamline testing, visualize AI’s thought process, and customize response handling for AI APIs, ensuring an efficient and transparent debugging process.

Oliver Kingsley

February 20, 2025

How to Build Websites Using Lovable AITutorials

How to Build Websites Using Lovable AI

Discover how to build any website using Lovable in this comprehensive guide. Learn step-by-step processes, explore innovative features, and integrate free tools like Apidog to streamline API management.

Ashley Innocent

February 19, 2025