Help with creating an environment variable with a where cause

Hi, I am hoping this is really simple. I am pretty new to postman.

I would like to capture the equipmentID only where the equipmentDescription = Run-flat tyres as an environment variable.

Below is an example of the JSON I am using:

"result": [
        {
            "id": "5c89a11e9fbed8000161ae1b",
            "equipment": {
                "equipmentItem": [
                    {
                        "equipmentId": "equipmentItem-5c89a11ea79a6",
                        "category": "option",
                        "equipmentCode": "258",
                        "equipmentDescription": "Run-flat tyres"
                    },
                    {
                        "equipmentId": "equipmentItem-5c89a11ea7a63",
                        "category": "option",
                        "equipmentCode": "465",
                        "equipmentDescription": "Through-loading system"
                    },
                    {
                        "equipmentId": "equipmentItem-5c89a11ea7b1e",
                        "category": "package",
                        "equipmentCode": "ZNB",
                        "equipmentDescription": "ABC Navigation"
                    }
                ]
            },
            "regNo": "XXXXXX"
        }
    ]

Many thanks in advance

Spencer

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:

{
    "result": [
        {
            "id": "5c89a11e9fbed8000161ae1b",
            "equipment": {
                "equipmentItem": [
                    {
                        "equipmentId": "equipmentItem-5c89a11ea79a6",
                        "category": "option",
                        "equipmentCode": "258",
                        "equipmentDescription": "Run-flat tyres"
                    },
                    {
                        "equipmentId": "equipmentItem-5c89a11ea7a63",
                        "category": "option",
                        "equipmentCode": "465",
                        "equipmentDescription": "Through-loading system"
                    },
                    {
                        "equipmentId": "equipmentItem-5c89a11ea7b1e",
                        "category": "package",
                        "equipmentCode": "ZNB",
                        "equipmentDescription": "ABC Navigation"
                    }
                ]
            },
            "regNo": "XXXXXX"
        }
    ]
}

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.

2 Likes


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;
    }
}
1 Like

Thanks, worked a treat! That has made my life 100% easier.

You’re very welcome mate.

Using Find, Filter and Maps have been a massive help in my Postman tests.

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:

var searchItemVar = ‘Run-flat tyres’;

pm.environment.set(“searchItemVar”, searchItemVar);

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?

This was how I hoped it would work


Thanks

Yep!

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:

pm.environment.get("searchItemVar");

So line 15 would read like this:

const runFlatTypesItem = equipmentItems.find(function(equipmentItem) {
    return equipmentItem.equipmentDescription == pm.environment.get("searchItemVar");
});

If you want to use environment variables in the URL, Headers and other places you use {{searchItemVar}}

Have fun :slight_smile: