# Help with creating an environment variable with a where cause

**URL:** <https://community.postman.com/t/help-with-creating-an-environment-variable-with-a-where-cause/5979>\
**Category:** Help Hub\
**Created:** [June 19, 2019, 11:01am UTC](https://community.postman.com/t/help-with-creating-an-environment-variable-with-a-where-cause/5979 "2019-06-19T11:01:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![spencer.jerome](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@spencer.jerome](https://community.postman.com/u/spencer.jerome)\
**Post date:** [June 19, 2019, 11:01am UTC](https://community.postman.com/t/help-with-creating-an-environment-variable-with-a-where-cause/5979/1 "2019-06-19T11:01:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![postman-paul540](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/postman-paul540/32/149_2.png) [@postman-paul540](https://community.postman.com/u/postman-paul540)\
**Post date:** [June 19, 2019, 1:21pm UTC](https://community.postman.com/t/help-with-creating-an-environment-variable-with-a-where-cause/5979/2 "2019-06-19T13:21:27Z")

</div>

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:**

```auto
{
    "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:**

```auto
// 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](https://api.myjson.com/bins/114lr9)  
Save the contents of the above into a “.json” file and import it into Postman.

---

<div class="post-metadata">

**Author:** ![postman-paul540](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/postman-paul540/32/149_2.png) [@postman-paul540](https://community.postman.com/u/postman-paul540)\
**Post date:** [June 19, 2019, 1:24pm UTC](https://community.postman.com/t/help-with-creating-an-environment-variable-with-a-where-cause/5979/3 "2019-06-19T13:24:45Z")

</div>

…And just in case you’re not a fan of using the “find” function, here’s the above using a simple for loop:

```auto
// 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;
    }
}

```

---

<div class="post-metadata">

**Author:** ![spencer.jerome](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@spencer.jerome](https://community.postman.com/u/spencer.jerome)\
**Post date:** [June 19, 2019, 1:34pm UTC](https://community.postman.com/t/help-with-creating-an-environment-variable-with-a-where-cause/5979/4 "2019-06-19T13:34:59Z")

</div>

> [@postman-paul540](#):
>
> // 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); }

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

---

<div class="post-metadata">

**Author:** ![postman-paul540](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/postman-paul540/32/149_2.png) [@postman-paul540](https://community.postman.com/u/postman-paul540)\
**Post date:** [June 19, 2019, 2:12pm UTC](https://community.postman.com/t/help-with-creating-an-environment-variable-with-a-where-cause/5979/5 "2019-06-19T14:12:00Z")

</div>

You’re very welcome mate.

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

---

<div class="post-metadata">

**Author:** ![spencer.jerome](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@spencer.jerome](https://community.postman.com/u/spencer.jerome)\
**Post date:** [June 19, 2019, 6:58pm UTC](https://community.postman.com/t/help-with-creating-an-environment-variable-with-a-where-cause/5979/6 "2019-06-19T18:58:42Z")

</div>

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…

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/1/3/13a3bff672ef6fb3c19bc7c23f278e0c0978f551.png)

Thanks

---

<div class="post-metadata">

**Author:** ![postman-paul540](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/postman-paul540/32/149_2.png) [@postman-paul540](https://community.postman.com/u/postman-paul540)\
**Post date:** [June 19, 2019, 9:26pm UTC](https://community.postman.com/t/help-with-creating-an-environment-variable-with-a-where-cause/5979/7 "2019-06-19T21:26:31Z")

</div>

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:

```auto
pm.environment.get("searchItemVar");

```

So line 15 would read like this:

```auto
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 🙂
