I have successfully created a POSTMAN call to a server that provides a JSON response containing a list of items as shown below:-
{
"count": 6909,
"setIds": [
"1/7842/0889#001",
"2/4259/0166#001",
"ENT0730/0009",
"9D/11181#002",
"1/9676/0001#001",
"2/4718/0001#004",
"2/1783/0044#001",
"1/4501/0001#001",
"1/2028/0002#002",
"2/3120/0079#001",
"2/1672/0024#001",
"2/3398/0064#001"
}
My objective now is to make subsequent calls to another server using the setID values and iterate through each item to cross-verify the responses. However, the challenge I encounter is that the second server requires the set id format with forward slashes converted to underscores and hashes to dots, such as:
"1/7842/0889#001"
should transform into
"1_7842_0889.001"
I have implemented code in POSTMAN to perform this conversion:
var jsonData = pm.response.json()
for (var i=0; i<jsonData.setIds.length; i++)
{
var new_j = jsonData.setIds[i].replace (/#/g, ".");
var new_i = new_j.replace (/\//g, "_");
}
})
While this code executes correctly line by line generating the desired output in the POSTMAN console, my main goal is to save the entire JSON data in the appropriate format to a file and then retrieve it line by line with modified data. Unfortunately, I am facing difficulties saving the data in the required form using my existing approach, indicating a probable oversight on my part. Is there a method to write content to a file incrementally within POSTMAN or via a scripted solution while manipulating the data during creation?
An alternative strategy could involve reading from the saved JSON response directly, processing the information using a pre-request script.
In an attempt to resolve this issue, I experimented with environmental variables. For example, in the initial call, I employed the following script:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable('setIds', JSON.stringify(jsonData));
Subsequently, my intention was to utilize this environment variable in a pre-request script for the second call to the express server where the payload is forwarded. Regrettably, this approach failed due to syntax errors, specifically encountering SyntaxError: Unexpected token {
.
While exploring potential solutions, one possibility involves handling these procedures outside of POSTMAN using JavaScript. However, I feel slightly overwhelmed by the starting point. Any guidance or recommendations would be greatly appreciated.