I need to extract a value from the response and store in an Environment Variable however the value is another {key:value} pair. Below was the response and specifically I wanted to extract the value “11c4293c-c807-4d68-ae79” under the “fileId” key under the “progress” key.
My Test script below is getting the value of {“fileId”:“11c4293c-c807-4d68-ae79”} saved in the Environment.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable(“fileID”, jsonData.response.progress);
So, I changed it to below but I am not getting anything.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable(“fileID”, jsonData.response.progress[“fileId”]);
@w4dd325 thank you for responding to my inquiry. I tried to follow your recommendation and still did not get anything maybe because of the unconventional format of the progress key value? ; the console log was giving me undefined. Below was the actual response.
@w4dd325 I really appreciate your help, it worked. I have no background in Javascript so I have to google the ‘const’ keyword you’ve used, the ‘pm.environment.set’ method and the slicing. I am just puzzled in the slicing because based on your start parameter which is 11 seems like the \ character was not included in the count. Anyway, I tried changing the values to check different output.
I think that is because the \ character is considered an “escape” character meaning that if you put \" then the " (double quotes) are read as text and not as part of the JSON.
The const is for constant, its a variable type that wont change, the alternative would be “let” if the variable is likely to change during your code.
Slice is pretty much just "cut off this many letters from the front of the string and this many from the end of the string.
pm.environment.set
is the newer way of doing postman.setEnvironmentVariable
It’s to do with the API thats used by Postman.
postman. = old
pm. = new
Hope that helps, happy to help if you get stuck.
(I’m a newbie with JS too)