# Can't Parse Specific Variable from Response Body

**URL:** <https://community.postman.com/t/cant-parse-specific-variable-from-response-body/24056>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [May 24, 2021, 9:24pm UTC](https://community.postman.com/t/cant-parse-specific-variable-from-response-body/24056 "2021-05-24T21:24:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![frankles](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/frankles/32/14635_2.png) [@frankles](https://community.postman.com/u/frankles)\
**Post date:** [May 24, 2021, 9:24pm UTC](https://community.postman.com/t/cant-parse-specific-variable-from-response-body/24056/1 "2021-05-24T21:24:05Z")

</div>

_My question_:  
I’m working on chaining calls together to take the copy/pasting pain out of testing. I’m trying to pull a value from the JSON response body, save it as an Environment Variable which I can then use in another call.

My issue is that I can’t seem to get the value I need populated in my environment variables. It’s not just this specific variable; I can’t get any of them to show up.

I’ve tried a couple approaches, courtesy of Postman documentation, which I’ll paste below. The name of the variable shows up in the Environment Variables, but the value is always null.

In the Tests tab:

```auto
// Access the JSON response body
let response = pm.response.json();

// Extract usr_key from response
let usrKey = response.users.id;

pm.environment.set("usrKey", usrKey);

```

```auto
// Alternate approach, also fails.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("id", jsonData.users.id);

```

\*Here’s an example of a response. The value I’m attempting to extract is the 7-digit “id”:

```auto
{
    "links": [
        {
            "rel": "self",
            "href": "http://<OIMURL>:14000/iam/governance/selfservice/api/v1/users"
        }
    ],
    "users": 
        {
            "links": [
                {
                    "rel": "self",
                    "href": "http://<OIMURL>:14000/iam/governance/selfservice/api/v1/users/1145208"
                }
            ],
            "id": "1145208",
            "status": "SUCCESS",
            "requestId": "1370",
            "name": "TEST"
        }
    ]
}

```

_I’ve already tried_:

I’ve tried setting the variables in the script as users.id, id, and some silly shot in the dark options. None worked

---

<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:** [May 24, 2021, 9:42pm UTC](https://community.postman.com/t/cant-parse-specific-variable-from-response-body/24056/2 "2021-05-24T21:42:29Z")

</div>

The reference to the value in the response seems fine - Have you already created the Environment file and set this to the active environment?

Can you confirm that the example you posted is correct as it does not look like valid JSON?

Taking a guess - can you try `response.users[0].id`?

Are you able to share a full screen image of the app so we can see what that looks like?

---

<div class="post-metadata">

**Author:** ![frankles](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/frankles/32/14635_2.png) [@frankles](https://community.postman.com/u/frankles)\
**Post date:** [May 24, 2021, 9:49pm UTC](https://community.postman.com/t/cant-parse-specific-variable-from-response-body/24056/3 "2021-05-24T21:49:48Z")

</div>

Sure, here’s a screenshot. Let me know if I missed anything. (You know, like the point…)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/d/9d1652fdc3fed4ff14a4c55194319070a5f5d134.png)

---

<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:** [May 24, 2021, 9:50pm UTC](https://community.postman.com/t/cant-parse-specific-variable-from-response-body/24056/4 "2021-05-24T21:50:53Z")

</div>

The example wasn’t correct and `users` is an array:

> [@danny-dainton](#):
>
> Taking a guess - can you try `response.users[0].id` ?

---

<div class="post-metadata">

**Author:** ![frankles](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/frankles/32/14635_2.png) [@frankles](https://community.postman.com/u/frankles)\
**Post date:** [May 24, 2021, 9:53pm UTC](https://community.postman.com/t/cant-parse-specific-variable-from-response-body/24056/5 "2021-05-24T21:53:13Z")

</div>

Yeah, I can confirm, the JSON formatting is exactly what I’m getting. I thought it looked a little janky.

It looks like your unorthodox but obviously necessary formatting suggestion worked. Thank you so much for your assistance.

---

<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:** [May 24, 2021, 9:55pm UTC](https://community.postman.com/t/cant-parse-specific-variable-from-response-body/24056/6 "2021-05-24T21:55:03Z")

</div>

I can see from the image of the app that it was an array but the example, had an extra `] ` in a position that would suggest it was from `users` 😁

---

<div class="post-metadata">

**Author:** ![neilstudd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/neilstudd/32/8213_2.png) [@neilstudd](https://community.postman.com/u/neilstudd)\
**Post date:** [May 24, 2021, 9:56pm UTC](https://community.postman.com/t/cant-parse-specific-variable-from-response-body/24056/7 "2021-05-24T21:56:55Z")

</div>

> [@frankles](#):
>
> unorthodox but obviously necessary formatting

It’s very orthodox in your scenario 😉 The square brackets at the start and end of the `users` section indicates that technically your response could contain _multiple_ users (therefore you have to specify `[0]` because in Javascript-land that means “the first item in the list”).

It’s worth bearing in mind, just in case you have edge-cases where multiple users come back and you might not necessarily want to grab the id of the first one.

---

<div class="post-metadata">

**Author:** ![robminardi](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/robminardi/32/5857_2.png) [@robminardi](https://community.postman.com/u/robminardi)\
**Post date:** [May 4, 2022, 5:59pm UTC](https://community.postman.com/t/cant-parse-specific-variable-from-response-body/24056/8 "2022-05-04T17:59:48Z")

</div>

If you do have a situation where you are storing more than 1 item in the array, just store the whole array as a variable and loop through it later. Using only index 0, as neilstudd said, will only get you the first one.

For example, if a company puts jobs in trays and they want the entire list of active trays in an array it might look something like this:

```auto
//Gets only the non-null jobID's (tray numbers) and puts them into an array.
The JS masters may have a way better way to do this, but this is how we currently do it.

    let trayIDs = response.data.order.filter(function (order) {

        if(order.jobID == ""){

            return false;

        }

        return true;

    }).map(order => order.jobID);

    pm.environment.set("trayIDs", JSON.stringify(trayIDs));

```
