Extract value from a JSON Response Body via the Test Scripts

Hey @jgreene7930,

Welcome to the community! :star:

Looking at your example response data - I can see that resources is an array which contains an object.

You would need to use resources[0].name to access the first object in the array.

You were on the right lines with logging the response to the Postman Console. This is what it would have logged:

Screenshot 2020-04-09 at 09.27.10

This is showing you that resources is an array with 1 object and if you were to expand this, you will see that the objects are zero-indexed. The first object would be 0 and that’s the reference that’s been added above [0].

So the full statement to set the variable would be:

let responseData = pm.response.json();
pm.environment.set("HostsToRemove", responseData.resources[0].name);
4 Likes