# How to select a specific string value with a condition as new variable?

**URL:** <https://community.postman.com/t/how-to-select-a-specific-string-value-with-a-condition-as-new-variable/9002>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [November 14, 2019, 10:45am UTC](https://community.postman.com/t/how-to-select-a-specific-string-value-with-a-condition-as-new-variable/9002 "2019-11-14T10:45:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![illusorydannyboy](https://avatars.discourse-cdn.com/v4/letter/i/8e8cbc/32.png) [@illusorydannyboy](https://community.postman.com/u/illusorydannyboy)\
**Post date:** [November 14, 2019, 10:45am UTC](https://community.postman.com/t/how-to-select-a-specific-string-value-with-a-condition-as-new-variable/9002/1 "2019-11-14T10:45:14Z")

</div>

I am writing a test that needs to find a line that has the “Square” in the “name” and set the “id” of the array that has that line as the new variable “departmentID”.

Response body

```
{
    "id": 1000000019,
    "name": "Exquisite",
    "city": "not important",
    "address": "not important",
    "tel": "not important"
},
{
    "id": 1000000006,
    "name": "Blue Square",
    "city": "not important",
    "address": "not important",
    "tel": "not important"
},
{
    "id": 1000000018,
    "name": "Big House",
    "city": "not important",
    "address": "not important",
    "tel": "not important"
}

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [November 14, 2019, 11:19am UTC](https://community.postman.com/t/how-to-select-a-specific-string-value-with-a-condition-as-new-variable/9002/2 "2019-11-14T11:19:06Z")

</div>

Hey @illusorydannyboy,

Welcome to the community! 👋

I’m not sure if that’s the complete response body or not, I would expect those objects to be wrapped in an array.

If it looked like this:

```
[
	{
	    "id": 1000000019,
	    "name": "Exquisite",
	    "city": "not important",
	    "address": "not important",
	    "tel": "not important"
	},
	{
	    "id": 1000000006,
	    "name": "Blue Square",
	    "city": "not important",
	    "address": "not important",
	    "tel": "not important"
	},
	{
	    "id": 1000000018,
	    "name": "Big House",
	    "city": "not important",
	    "address": "not important",
	    "tel": "not important"
	}
]

```

I would this add this to the `Tests` tab to loop through the array and grab the `id` from the object that has `Square` within the `name`:

```
_.each(pm.response.json(), (item) => {
    if(item.name.includes('Square')) {
        pm.environment.set('departmentID', item.id)
    }
}) 

```

This is only really going the work if you have a single object in the array with `Square` in the `name` property if you had more than one occurrence, it’s just going to overwrite the variable and set the last one. You _could_ add something to the variable name as a unique value to stop that from happening.

---

<div class="post-metadata">

**Author:** ![illusorydannyboy](https://avatars.discourse-cdn.com/v4/letter/i/8e8cbc/32.png) [@illusorydannyboy](https://community.postman.com/u/illusorydannyboy)\
**Post date:** [November 14, 2019, 11:35am UTC](https://community.postman.com/t/how-to-select-a-specific-string-value-with-a-condition-as-new-variable/9002/3 "2019-11-14T11:35:19Z")

</div>

Thank you for the solution!
