0
0
Postmantesting~15 mins

Extracting data from responses in Postman - Build an Automation Script

Choose your learning style9 modes available
Extract user ID from API response and verify it
Preconditions (2)
Step 1: Send a GET request to https://api.example.com/users/123
Step 2: Wait for the response
Step 3: Extract the 'id' field from the JSON response body
Step 4: Verify that the extracted 'id' equals 123
✅ Expected Result: The 'id' field is extracted successfully and equals 123
Automation Requirements - Postman Tests (JavaScript)
Assertions Needed:
Verify response status code is 200
Verify extracted 'id' field equals 123
Best Practices:
Use pm.response.json() to parse JSON response
Use pm.test() for assertions
Use descriptive test names
Avoid hardcoding values except for verification
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

const responseJson = pm.response.json();
const userId = responseJson.id;

pm.test('User ID is 123', () => {
    pm.expect(userId).to.eql(123);
});

The first test checks that the response status code is 200, which means the request was successful.

Then, we parse the JSON response using pm.response.json() to get a JavaScript object.

We extract the id field from the response object and store it in userId.

Finally, we assert that userId equals 123 using pm.expect().to.eql(). This confirms the correct data was returned.

Common Mistakes - 3 Pitfalls
Not parsing the response JSON before accessing fields
Using incorrect assertion syntax like pm.expect(userId == 123)
Not checking response status before extracting data
Bonus Challenge

Now add tests to extract and verify 'username' and 'email' fields from the response

Show Hint