We extract data from responses to use it in later tests or requests. This helps us check if the system works correctly step-by-step.
Extracting data from responses in Postman
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Postman
pm.test("Extract value from response", function () { var jsonData = pm.response.json(); pm.environment.set("keyName", jsonData.data.id); });
Use pm.response.json() to parse the response body as JSON.
Use pm.environment.set("key", value) to save data for later use.
Examples
token from the response into an environment variable called userToken.Postman
pm.test("Save user token", function () { var jsonData = pm.response.json(); pm.environment.set("userToken", jsonData.token); });
order.id into a collection variable named orderId.Postman
pm.test("Extract order ID", function () { var jsonData = pm.response.json(); pm.collectionVariables.set("orderId", jsonData.order.id); });
Postman
pm.test("Check response status", function () { pm.response.to.have.status(200); });
Sample Program
This test extracts the user.id from the JSON response and saves it as an environment variable userID. It also checks that the ID is a number.
Postman
pm.test("Extract user ID from response", function () { var jsonData = pm.response.json(); pm.environment.set("userID", jsonData.user.id); pm.expect(jsonData.user.id).to.be.a('number'); });
Important Notes
Always check the response format before extracting data to avoid errors.
Use environment or collection variables to share data between requests.
Use descriptive variable names to keep your tests clear.
Summary
Extracting data helps connect multiple API requests in tests.
Use pm.response.json() to read JSON responses.
Save data with pm.environment.set() or pm.collectionVariables.set().
Practice
1. What is the primary purpose of extracting data from API responses in Postman?
easy
Solution
Step 1: Understand the role of data extraction
Extracting data allows you to capture values from one response to use later.Step 2: Connect API requests using extracted data
This helps chain requests by passing data like tokens or IDs forward.Final Answer:
To reuse data in subsequent API requests -> Option AQuick Check:
Extract data = reuse in next requests [OK]
Hint: Extract data to pass info between requests [OK]
Common Mistakes:
- Thinking extraction changes the URL
- Confusing extraction with header modification
- Believing extraction deletes data
2. Which Postman script correctly extracts the value of
userId from a JSON response and saves it as an environment variable?easy
Solution
Step 1: Use pm.response.json() to parse JSON
This method converts the response body into a JavaScript object.Step 2: Use pm.environment.set() to save variable
Set the environment variable 'userId' with the extracted value.Final Answer:
let data = pm.response.json(); pm.environment.set('userId', data.userId); -> Option AQuick Check:
Parse JSON + set env variable = let data = pm.response.json(); pm.environment.set('userId', data.userId); [OK]
Hint: Use pm.response.json() then pm.environment.set() [OK]
Common Mistakes:
- Using pm.response.set() which doesn't exist
- Using pm.environment.get() to set variables
- Not parsing JSON before accessing properties
3. Given the response body:
What will this Postman script save in the environment variable
{"token": "abc123", "user": {"id": 42}}What will this Postman script save in the environment variable
authToken?
let jsonData = pm.response.json();
pm.environment.set('authToken', jsonData.token);medium
Solution
Step 1: Parse the JSON response
jsonData.token accesses the 'token' key which has value "abc123".Step 2: Set environment variable with token value
pm.environment.set saves "abc123" as 'authToken'.Final Answer:
"abc123" -> Option DQuick Check:
jsonData.token = "abc123" [OK]
Hint: Access exact key from parsed JSON to get value [OK]
Common Mistakes:
- Using user.id instead of token
- Expecting number 42 instead of string token
- Not parsing JSON before accessing token
4. You wrote this Postman test script to extract
But the environment variable
sessionId from the response:let data = pm.response.json();
pm.environment.set('sessionId', data.session_id);But the environment variable
sessionId is always empty. What is the likely problem?medium
Solution
Step 1: Check JSON key names carefully
The script uses 'session_id' but the response likely has 'sessionId' (camelCase).Step 2: Correct key name to match response
Use data.sessionId to correctly extract the value.Final Answer:
The response JSON usessessionIdnotsession_id-> Option CQuick Check:
Key name mismatch causes empty variable [OK]
Hint: Match JSON keys exactly, including case [OK]
Common Mistakes:
- Assuming pm.environment.set() doesn't work
- Not parsing JSON before accessing keys
- Confusing environment and collection variables
5. You receive this nested JSON response:
How do you extract and save the name of the second user as a collection variable in Postman?
{"data": {"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}}How do you extract and save the name of the second user as a collection variable in Postman?
hard
Solution
Step 1: Parse the nested JSON response
Access the array at json.data.users and select index 1 for the second user.Step 2: Save the second user's name as a collection variable
Use pm.collectionVariables.set with key 'secondUserName' and value json.data.users[1].name.Final Answer:
let json = pm.response.json(); pm.collectionVariables.set('secondUserName', json.data.users[1].name); -> Option BQuick Check:
Index 1 in users array = second user name [OK]
Hint: Use zero-based index and correct variable scope [OK]
Common Mistakes:
- Using index 2 instead of 1 for second user
- Mixing environment and collection variables
- Not parsing JSON before accessing nested data
