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.

“response”: {
“startTime”: 1655525860907,
“version”: 1655525861235,
“progress”: “{“fileId”:“11c4293c-c807-4d68-ae79”}”,
“endTime”: 1655525861235,
“isError”: false,
“lastUpdate”: 1655525861235,
“rootId”: “e639f2a4-5aa5-43ea-8222”,
“username”: “user”,
“serviceType”: “Command Runner Service”,
“instanceTenantId”: “5e8e896e4d4add”,
“id”: “e639f2a4-5aa5-43ea”
“version”: “1.0”

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”]);

Hi @rogelio.villaver

You have a mixture of (dot) and bracket notation… I think if you change your [“fileId”] part to (dot) notation it should work fine.

(The JSON you copy/pasted appears to be slightly malformed too, so assuming that is correct in your Postman response, you can use this…)

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("fileID", jsonData.response.progress.fileId);


Environment variable;

@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.

The ‘progress’ property holds a string, not another property, so in that case, you would have to manipulate the string;

const jsonData = JSON.parse(responseBody);
pm.environment.set("fileID", jsonData.response.progress.slice(11, -2));
console.log(jsonData.response.progress.slice(11, -2));

Or with a RegEx;

const jsonData = JSON.parse(responseBody);
let fieldIdRegEx = jsonData.response.progress.match(/{\"fileId\":\"(.*?)\"}/g);
console.log(fieldIdRegEx[0].slice(11, -2));
1 Like

@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.

is the newer way of doing
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)

1 Like