# Trouble setting environment variable from response

**URL:** <https://community.postman.com/t/trouble-setting-environment-variable-from-response/8927>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [November 11, 2019, 7:48pm UTC](https://community.postman.com/t/trouble-setting-environment-variable-from-response/8927 "2019-11-11T19:48:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![greyson.hobbs](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@greyson.hobbs](https://community.postman.com/u/greyson.hobbs)\
**Post date:** [November 11, 2019, 7:48pm UTC](https://community.postman.com/t/trouble-setting-environment-variable-from-response/8927/1 "2019-11-11T19:48:11Z")

</div>

I’m trying to store the ID from a response body to use in an another API push, but it is isn’t picking up the value I need. I have an environment variable for “userId” and I want the test script to set the value for that to the “id” value. Whenever I run this test though, the value doesn’t get updated.

Here is the test I am using:

```
var jsonData = JSON.parse(responseBody);

pm.environment.set("userId", jsonData.id);

```

The response body looks like this:

```
[
        {
            "id": "64d4dgtwac6hSTWaG4s6",
            "status": "ACTIVE",
            "created": "2016-06-02T15:06:16.000Z",
            "lastLogin": "2018-12-11T16:51:11.000Z",
            "profile": {
                "lastName": "Doe",
                "displayName": "John Doe",
                "firstName": "John",
            },
            "credentials": {
                "provider": {
                    "type": "ACTIVE_DIRECTORY",
                    "name": "domain.com"
        }
    ]

```

---

<div class="post-metadata">

**Author:** ![mick-mcbride](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mick-mcbride/32/783_2.png) [@mick-mcbride](https://community.postman.com/u/mick-mcbride)\
**Post date:** [November 11, 2019, 10:04pm UTC](https://community.postman.com/t/trouble-setting-environment-variable-from-response/8927/2 "2019-11-11T22:04:29Z")

</div>

@greyson.hobbs

Try pm.environment.set(“userId”, jsonData[0].id);

Mick

---

<div class="post-metadata">

**Author:** ![greyson.hobbs](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@greyson.hobbs](https://community.postman.com/u/greyson.hobbs)\
**Post date:** [November 12, 2019, 1:37am UTC](https://community.postman.com/t/trouble-setting-environment-variable-from-response/8927/3 "2019-11-12T01:37:51Z")

</div>

Thanks for the quick response. I tried that and it came back with this error.

There was an error in evaluating the test script: SyntaxError: Invalid or unexpected token

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [November 12, 2019, 4:16am UTC](https://community.postman.com/t/trouble-setting-environment-variable-from-response/8927/4 "2019-11-12T04:16:21Z")

</div>

As @mick-mcbride suggested, if your response looks like exactly like you posted above, then you can try this:

```javascript
let resp = pm.response.json();

pm.environment.set('userId', resp[0].id);

```

If that doesn’t work, then can you paste a screenshot of your response?

---

<div class="post-metadata">

**Author:** ![greyson.hobbs](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@greyson.hobbs](https://community.postman.com/u/greyson.hobbs)\
**Post date:** [November 12, 2019, 12:16pm UTC](https://community.postman.com/t/trouble-setting-environment-variable-from-response/8927/5 "2019-11-12T12:16:56Z")

</div>

That worked! Thank you both for you help!

---

<div class="post-metadata">

**Author:** ![greyson.hobbs](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@greyson.hobbs](https://community.postman.com/u/greyson.hobbs)\
**Post date:** [November 12, 2019, 1:22pm UTC](https://community.postman.com/t/trouble-setting-environment-variable-from-response/8927/6 "2019-11-12T13:22:09Z")

</div>

How would I get the same thing to work for this? I want to pull all of the items under the “Groups” into a variable. I tried the same answer you provided for getting the ID, but it didn’t work as expected.

```
[
    {
        "id": 2457585,
        "name": "john.doe@domain.com",
        "groups": [
            {
                "id": 9568265,
                "name": "Main Users"
            },
            {
                "id": 2653481,
                "name": "New Group"
            },
            {
                "id": 6475824,
                "name": "Help Desk"
            },
            {
                "id": 6958234,
                "name": "Everyone"
            },
            {
                "id": 9854162,
                "name": "(Managers and Supervisors)"
            }
        ]
    }
]
```

---

<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 12, 2019, 2:39pm UTC](https://community.postman.com/t/trouble-setting-environment-variable-from-response/8927/7 "2019-11-12T14:39:49Z")

</div>

You could try something like this to get that array and set it as an environment variable:

`pm.environment.set("groupItemsArray", JSON.stringify(resp[0].groups))`

To get each of the `id` values, you would need to loop through them and then set that array as the environment variable:

```
let groupIds = []

_.each(resp[0].groups, (item) => {
    groupIds.push(item.id)
})

pm.environment.set("groupIds", JSON.stringify(groupIds))

```

Is this what you were looking to do?
