The cool thing about writing code is that there are so many different ways to get the result you want.
Hereâs the first thing I could come up with.
Hereâs a code example, as well as a request you can import into postman. Youâll have to create an environment though so the environment variable can be saved (or you can just use an existing one)
So, Iâm going to assume this is your response body:
Hereâs the code I have in my âTestsâ tab for the above response body:
// Parse the response body as JSON
const body = pm.response.json();
//Assign the "equipmentItem" array to a variable to make the tests look tidier.
const equipmentItems = body.result[0].equipment.equipmentItem;
// Perform a find on the above array.
// It looks through each item in the array
// If the item's 'equipmentDescription' is 'Run-flat tyres', then save that entire to a NEW array and end the 'find' function
const runFlatTypesItem = equipmentItems.find(function(equipmentItem) {
return equipmentItem.equipmentDescription == "Run-flat tyres";
});
// If the above 'runFlatTypesItem' array DID NOT find a description that was "Run-flat tyres"
// Then the variable runFlatTypesItem would be 'undefined'
// So we're saying if it's NOT undefined, then it must have "Run-flat tyres" as the description
// So we then save that equipmentId that was linked to "Run-flat tyres" as environment variable
if(runFlatTypesItem !== undefined) {
pm.environment.set("equipmentId", runFlatTypesItem.equipmentId);
}
Link to JSON file: https://api.myjson.com/bins/114lr9
Save the contents of the above into a â.jsonâ file and import it into Postman.
âŚAnd just in case youâre not a fan of using the âfindâ function, hereâs the above using a simple for loop:
// Parse the response body as JSON
const body = pm.response.json();
//Assign the "equipmentItem" array to a variable to make the tests look tidier.
const equipmentItems = body.result[0].equipment.equipmentItem;
// Loop through the 'equipmentItems' array.
// If the 'equipmentDescription' is 'Run-flat tyres'
// Save the equipmentId as an environment variable
// Then 'break' out of the for loop as there's no need to continue the loop now we have what we want :)
for(let i = 0; i < equipmentItems.length; i++) {
if(equipmentItems[i].equipmentDescription == "Run-flat tyres") {
pm.environment.set("equipmentId", equipmentItems[i].equipmentId);
break;
}
}
Actually I have another question on this if you dont mind⌠Can I set a variable in my pre-request script where I can change the âRun-flat tyreâ value to something else i.e âLeather steering wheelâ that I might want to search for instead?
So for instance, I currently have a set variables script which I have variables set like:
And then I would usually be able to reference this in the body by using â{{searchItemVar}}â, so was hoping I could do something similar to enable me to have one collection which I change the variable in the set variables script to make it more flexible. Hopefully I am making some sense?
Youâre halfway correct.
If you want to use environment variables in the âPre-Request Scriptâ and âTestsâ tabs, you call them by using the below: