JSON Response as below:
{ "value":[ { "email": "xxx@xx.com" }, { "email": "xxx@xx.com" }, { "email": "xxx@xx.com" }, { "email": "xxx@xx.com" } ] }
Tried below code snippet:
const responseBody = '{ "value":[ { "email": "xxx@xx.com" }, { "email": "xxx@xx.com" }, { "email": "xxx@xx.com" }, { "email": "xxx@xx.com" } ] }';
var data = JSON.parse(responseBody);
const temp = [];
data.forEach(function(value, i){
if(data.value[i]){
temp[i] = data.value[i].email
}
});
var subscriptions = temp.join(",");
postman.setEnvironmentVariable("testData", temp);
data is an object, not an array. It doesn't have a forEach method. You have to iterate over data.value:
const responseBody = '{ "value":[ { "email": "xxx@xx.com" }, { "email": "xxx@xx.com" }, { "email": "xxx@xx.com" }, { "email": "xxx@xx.com" } ] }';
var data = JSON.parse(responseBody);
const temp = data.value.map(value => value?.email);
var subscriptions = temp.join(",");
console.log(subscriptions);
//postman.setEnvironmentVariable("testData", temp);