What if your tests could remember and share important data all by themselves?
Why Extracting data from responses in Postman? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine testing an app where you have to check many API responses by hand. You open each response, look for the data you need, and write it down or copy it manually.
This feels like searching for a needle in a haystack every time you test.
Doing this manually is slow and tiring. You might miss important details or make mistakes copying data. It's hard to keep track of what you found, especially when responses change often.
Manual checks also waste time that could be used for more important testing.
Extracting data from responses automatically lets you grab exactly what you need with a few lines of code. Postman can save values from responses and reuse them in other tests or requests.
This makes testing faster, more accurate, and less boring.
Look at response body, find user ID, copy it manually
let userId = pm.response.json().user.id; pm.environment.set('userId', userId);It enables smooth, automatic workflows where tests talk to each other by sharing data, making your testing smarter and faster.
When testing a signup API, you extract the new user's ID from the response and use it to test profile updates or deletions automatically, without typing anything.
Manual data extraction is slow and error-prone.
Automated extraction saves time and improves accuracy.
Extracted data can be reused to create powerful test flows.
Practice
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]
- Thinking extraction changes the URL
- Confusing extraction with header modification
- Believing extraction deletes data
userId from a JSON response and saves it as an environment variable?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]
- Using pm.response.set() which doesn't exist
- Using pm.environment.get() to set variables
- Not parsing JSON before accessing properties
{"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);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]
- Using user.id instead of token
- Expecting number 42 instead of string token
- Not parsing JSON before accessing token
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?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]
- Assuming pm.environment.set() doesn't work
- Not parsing JSON before accessing keys
- Confusing environment and collection variables
{"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?
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]
- Using index 2 instead of 1 for second user
- Mixing environment and collection variables
- Not parsing JSON before accessing nested data
