# Using postman.setEnvironmentVariable("host", jsonData.host) sets host to NULL

**URL:** <https://community.postman.com/t/using-postman-setenvironmentvariable-host-jsondata-host-sets-host-to-null/4708>\
**Category:** Help Hub\
**Created:** [March 26, 2019, 11:06am UTC](https://community.postman.com/t/using-postman-setenvironmentvariable-host-jsondata-host-sets-host-to-null/4708 "2019-03-26T11:06:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![psorense](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@psorense](https://community.postman.com/u/psorense)\
**Post date:** [March 26, 2019, 11:06am UTC](https://community.postman.com/t/using-postman-setenvironmentvariable-host-jsondata-host-sets-host-to-null/4708/1 "2019-03-26T11:06:04Z")

</div>

I want to extract a value from the response and store in a Variable. So I use:

[https://postman-echo.com/get](https://postman-echo.com/get)

Which gives me the Response:

```
{
    "args": {},
    "headers": {
        "x-forwarded-proto": "https",
        "host": "postman-echo.com",
        "accept": "*/*",
        "accept-encoding": "gzip, deflate",
        "cache-control": "no-cache",
        "cookie": "sails.sid=s%3A1e1Mmpz_0cZOHSvJ7PECnByndH6aCOkO.aKuAswovLqNDM%2FJsO7h6ptziVJZluWAuqC0gAUsEdlY",
        "postman-token": "e61900c7-7827-4078-b56d-ec0a50e6f7da",
        "user-agent": "PostmanRuntime/7.6.0",
        "x-forwarded-port": "443"
    },
    "url": "https://postman-echo.com/get"
}

```

And my Test script is:

```
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("host", jsonData.host);

```

Why is the Variable not set with the correct content?

Thanks

---

<div class="post-metadata">

**Author:** ![deepak.pathania](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@deepak.pathania](https://community.postman.com/u/deepak.pathania)\
**Post date:** [March 26, 2019, 12:35pm UTC](https://community.postman.com/t/using-postman-setenvironmentvariable-host-jsondata-host-sets-host-to-null/4708/2 "2019-03-26T12:35:53Z")

</div>

Hey @psorense. First of all, welcome to the community. 😄

Coming to your question, the `host` property is nested inside the `headers` object. You’re trying to access it directly from `jsonData`.

Here’s what your script should look like:

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

postman.setEnvironmentVariable("host", jsonData.headers.host);

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![psorense](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@psorense](https://community.postman.com/u/psorense)\
**Post date:** [March 27, 2019, 12:17pm UTC](https://community.postman.com/t/using-postman-setenvironmentvariable-host-jsondata-host-sets-host-to-null/4708/3 "2019-03-27T12:17:12Z")

</div>

Great! It works 🙂  
Would you also have a solution for how to get the value from a key that has spaces in name like:

```
"values": {
        "Request Number": "REQ000000023077"
}

```

`postman.setGlobalVariable("REQ_InstanceId", jsonData.values.Request Number);`

I’ve tried with [,{,’, and "  
But everything gives an error.

---

<div class="post-metadata">

**Author:** ![deepak.pathania](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@deepak.pathania](https://community.postman.com/u/deepak.pathania)\
**Post date:** [March 27, 2019, 12:23pm UTC](https://community.postman.com/t/using-postman-setenvironmentvariable-host-jsondata-host-sets-host-to-null/4708/4 "2019-03-27T12:23:58Z")

</div>

Hey @psorense. This should work.

```javascript
postman.setGlobalVariable("REQ_InstanceId", jsonData.values['Request Number']);

```

---

<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:** [February 28, 2020, 10:48am UTC](https://community.postman.com/t/using-postman-setenvironmentvariable-host-jsondata-host-sets-host-to-null/4708/6 "2020-02-28T10:48:27Z")

</div>

Hey @Alexios,

Welcome to the community 👋

As that property is inside an object in the `data` [array](https://www.w3schools.com/js/js_arrays.asp) (`[]`), you would need to add this to your reference path. The `data` array looks like it only has a single [object](https://www.w3schools.com/js/js_objects.asp) (`{}`) so you would reference that with `[0]` as that would be the first item in the zero-indexed list.

```
pm.test("Check if Device with currentStatus is online", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.data[0].currentStatus).to.eql("online");
});

```

The way you have it, it would be looking for the `currentStatus` property in the 17th object in the array of the response body.

Here’s an example of what that would look like:

```
[
    {
      // Object 1
    },
    {
      // Object 2
    },
    {
      // Object ...
    },
    {
      // Object 15
    },
    {
      // Object 16
    },
    {
      "currentStatus": "online"
    }
]

```

Hope that helps 🙂

---

<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:** [February 28, 2020, 12:35pm UTC](https://community.postman.com/t/using-postman-setenvironmentvariable-host-jsondata-host-sets-host-to-null/4708/8 "2020-02-28T12:35:07Z")

</div>

What is the reference you are making for that item? What does your test script look like?

---

<div class="post-metadata">

**Author:** ![Alexios](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@Alexios](https://community.postman.com/u/Alexios)\
**Post date:** [February 28, 2020, 12:41pm UTC](https://community.postman.com/t/using-postman-setenvironmentvariable-host-jsondata-host-sets-host-to-null/4708/9 "2020-02-28T12:41:12Z")

</div>

this is my script:

```
pm.test("Check if Device with udid is 62652651265asdasdasd", function () {
    var jsonData = pm.response.json();
    console.log(jsonData);
    pm.expect(jsonData[1].udid).to.eql("62652651265asdasdasd");
});
```

---

<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:** [February 28, 2020, 12:55pm UTC](https://community.postman.com/t/using-postman-setenvironmentvariable-host-jsondata-host-sets-host-to-null/4708/10 "2020-02-28T12:55:03Z")

</div>

As I mentioned in my first reply, that won’t work because those properties are in the `data` array.

You need to include that in the reference, take a look at the test script that I sent you.

---

<div class="post-metadata">

**Author:** ![Alexios](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@Alexios](https://community.postman.com/u/Alexios)\
**Post date:** [March 4, 2020, 10:37pm UTC](https://community.postman.com/t/using-postman-setenvironmentvariable-host-jsondata-host-sets-host-to-null/4708/11 "2020-03-04T22:37:43Z")

</div>

@danny-dainton  
Thanks a lot man for all ur help i solve the problem with ur help
