Access property from variable

Hello!

I need to be able to do something like that … is it possible?

var variable = “storm” //I get this value from a previous request
var jsonData = pm.response.json();
pm.globals.set (“type”, jsonData.rain.(pm.environment.get (“variable”));

Thank you!

Pablo.

Hi there @pablovok - looks like you want to parse a response and set a global variable based on an environment variable from the previous request.

// if you set the environment variable in the previous request like
pm.environment.set("variable", "storm")

// then this would work
var variable = pm.environment.get("variable");
var jsonData = pm.response.json();
pm.globals.set("type", jsonData.rain.variable);

// and if `jsonData.rain.variable` is an object, remember to encode it properly, like 
pm.globals.set("type", JSON.stringify(jsonData.rain.variable));

Thanks @jetison or your time and help.
I want to capture a value from the response, but build the path based on the values I capture from other responses.
Try what you mentioned above, but it doesn’t work for me.
The variable is not recognized as part of the path.

If you mean build a path in the URL, you can use the text syntax of double curly braces to access variables that are already defined, like:

https://example.com/{{value1}}/{{value2}}

If you mean build a path to use in the scripting section, can you string substitute (or concatenate) the values, like:

let value1 = pm.response.json().value1;
let value2 = pm.environment.get("value2");
let newPath = `https://example.com/${value1}/${value2}`

I’ll try to explain it with another example.
I have this json from answer::
{
“rain”: {
“storm”: {
“Day”: {
“One”: = 1
“Two”: = 2
}
},
“moderate”: {
"Day: {
“Four”: = 12
“Six”: = 22
}
}
}
}

instead of doing this:
pm.globals.set (“type”, jsonData.rain.storm.Day.One);

I would like to do something like this
pm.globals.set (“type”, jsonData.rain.(pm.environment.get (“variable”).Day.One);

Another problem I have is that postman does not recognize the object if the first letter is capitalized. Example “Day”

Thanks.

Pablo.

I already solved it, thanks!
JSON.stringify(jsonData.rain[pm.environment.get(“type”)].Day.One))