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

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

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

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

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:

var jsonData = JSON.parse(responseBody);

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

Hope this helps.

Great! It works :slight_smile:
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.

Hey @psorense. This should work.

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