Remove " from global variable, re-use in same request

in my 1st request under Tests i have the following to get a value and add it as a global variable for later use

var jsonData = pm.response.json();
var workplaceNumbers = [];

jsonData.workplaces.forEach(function(workplace) {
    var workplaceNumber = JSON.stringify(workplace.workplaceNumber);
    workplaceNumbers.push(workplaceNumber);
});
pm.globals.set("workplaceNumbers", (workplaceNumbers));

and this is an example of what workplaceNumbers saves: “1017703443”

in my 2nd request under Pre-request Script i try to remove the double-quotes, {{workplaceNumbers1}} is part of the Url of this 2nd request and double-quotes are an issue

// "Retrieve the saved "workplaceNumbers" value from the variable"*
var workplaceNumbers = pm.globals.get("workplaceNumbers");

// "Remove the double quotes from the workplaceNumbers value"
var workplaceNumbers1 = workplaceNumbers.replace(/"/g, "");

// "Set the new global variable "workplaceNumbers1"
pm.globals.set("workplaceNumbers1", workplaceNumbers1);

i am stuck here with the following error
There was an error in evaluating the Pre-request Script:TypeError: workplaceNumbers.replace is not a function

Please let me know if there is a better way of doing the above

Hey @normgean :wave:

Welcome to the Postman Community! :postman:

I’m not sure what the response payload looks like, an example of that would be great.

Would you need to do this JSON.stringify() here? That’s likely the step that’s adding the " around the stored array value.

Following on from Danny’s comment.

You need to use JSON.stringify when storing arrays and JSON.parse when retrieving them.

Hopefully this should sort the quotes issue.

If that doesn’t sort the issue, please paste an example response (with at least 2 records please).

I suspect you can also use the JavaScript filter function to get you the array of workPlaceNumber’s instead of the forEach loop. If you want help with that, then please post an example response.

On a side note, why are you setting this as a global variable instead of a collection or environment.

Do you really need this variable to be available to all of your collections in Postman.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.