# Getting a value from multiple arrays into a single environment variable

**URL:** <https://community.postman.com/t/getting-a-value-from-multiple-arrays-into-a-single-environment-variable/43310>\
**Category:** Help Hub\
**Created:** [January 27, 2023, 9:05pm UTC](https://community.postman.com/t/getting-a-value-from-multiple-arrays-into-a-single-environment-variable/43310 "2023-01-27T21:05:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![david.accola](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/david.accola/32/14490_2.png) [@david.accola](https://community.postman.com/u/david.accola)\
**Post date:** [January 27, 2023, 9:05pm UTC](https://community.postman.com/t/getting-a-value-from-multiple-arrays-into-a-single-environment-variable/43310/1 "2023-01-27T21:05:47Z")

</div>

What is the best way to get a value from multiple arrays into a single environment variable?

The JSON I’m working with has an array of arrays. I want to extract all occurrences of users.orgs.guids into an environment variable so I can test against it later. I’m thinking to store it comma separated. For example:

My JSON looks like this:

```auto
{
    "user": {
        "sourcedId": "foobar",
        "status": "foobar",
        "dateLastModified": "foobar",
        "username": "foobar",
        "givenName": "foobar",
        "middleName": "foobar",
        "familyName": "foobar",
        "role": "foobar",
        "identifier": "foobar",
        "grades": [
            "foobar"
        ],
        "userIds": [
            {
                "type": "foobar",
                "identifier": "foobar"
            }
        ],
        "orgs": [
            {
                "guid": "DC3CC5BB-43C6-4996-9618-866038C698BC",
                "href": "foobar",
                "type": "foobar"
            },
            {
                "guid": "912D34F0-A6E1-48BF-AF9D-2DCBFF259368",
                "href": "foobar",
                "type": "foobar"
            }
        ],
        "agents": [],
        "enabledUser": "foobar"
    }
}

```

Desired final result is an environment variable set to:  
DC3CC5BB-43C6-4996-9618-866038C698BC,912D34F0-A6E1-48BF-AF9D-2DCBFF259368

I’ll then search that variable later for a matching guid.

I’ve gotten as far as honing in on the guids I want with

```auto
var data = JSON.parse(responseBody)
data.user.orgs.forEach((element) => console.log((element.guid)))

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 27, 2023, 10:05pm UTC](https://community.postman.com/t/getting-a-value-from-multiple-arrays-into-a-single-environment-variable/43310/2 "2023-01-27T22:05:36Z")

</div>

Your response (once parsed) is an object that has multiple arrays.

The guid’s are in the orgs array (in a single array).

You can create a new array that contains the guids using the javascript map function.

Keep it as an array, its easier to assert against vs a comma separated string.

The test is then a simple includes.

As you have a single user in the response, you might as well parse directly to this element and call the variable user. It helps with legibility.

```auto
const user = pm.response.json().user; // parse directly to the user element

let guids = user.orgs.map(guids => guids.guid); // creates a new array of guids
// console.log(guids);

let guid = "912D34F0-A6E1-48BF-AF9D-2DCBFF259368";

pm.test(`guids contain ${guid}`, function () {
    pm.expect(guids).to.include(guid);
});

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/7/27b0e3d3cc71fddc37be20878e69439d3a1ba5ac.png)

If you are going to store them as environment or collection variables. You have to JSON.stringify() the value before storing it, and JSON.parse() it when retrieving. (Otherwise it will be treated as a string instead of an array).
