Log Response Issue

Hello all!,

I am having trouble setting a global variable based on the response of an API. The response format might be the issue but I was hoping to get some help or guidance please.

"[{\"ReportName\":\"03647_2023-09-22.zip\",\"ReportBlobUri\":\"https://testdomain.blob.core.windows.net/ioc-export/Entity=test/Target=testgateway/03647/03647_2023-09-22.zip?sv=2019-07-07&sr=b&sig=cCZ5p09MxRHh0z%2BUMYjBj6jeK21d4J9%2BPVO0wTl3azA%3D&st=2023-09-25T21%3A49%3A55Z&se=2023-09-25T22%3A59%3A55Z&sp=r\"},{\"ReportName\":\"03647_2023-09-23.zip\",\"ReportBlobUri\":\"https://testdomain.blob.core.windows.net/ioc-export/Entity=test/Target=testgateway/03647/03647_2023-09-23.zip?sv=2019-07-07&sr=b&sig=2XgjdMqoX%2FaVoaUsahy9y8IZ2QgD8FpCt%2Bi3326G2hw%3D&st=2023-09-25T21%3A49%3A55Z&se=2023-09-25T22%3A59%3A55Z&sp=r\"}]"

How can I parse or stringify the response? The problem is that the response has the same names, e.g. ReportName and ReportBlobUri. I want to essentially store them all as a global variable.

Any assistance would be greatly appreciated!

Thanks,

Hey @mitraqsr :wave:

Welcome to the Postman Community! :postman:

You could do something basic like this, to parse the response and then loop through it. This is the using the loop index to create a unique variable name or it would just override the last one it the name was the same.

let parsedData = JSON.parse(pm.response.text());

_.each(parsedData, (obj, index) => {
    pm.globals.set(`ReportName_${index}`, obj.ReportName);
    pm.globals.set(`ReportBlobUri_${index}`, obj.ReportBlobUri);
});

I’m sure there are better and more efficient ways to do this but I wanted to give you something that might help you out in the short term.

1 Like

Why a global variable?

Do you really want these variables\values available to all collections in Postman?

Wouldn’t a collection or environment be a better scope?

Your response appears to be a stringified array, so you just need to JSON.parse the response (twice) which should return you the underlying array and the method from Danny for iterating the array and using the array index and backticks\string literals to ensure the variables are unique seems to do the trick.

1 Like

Hi Danny,

Thanks for your response my friend. Its strange because your screenshot looks good and its exactly what I needed to achieve but when I copied your code and I tried it, it created 13k worth of variables that were just all empty so I cleared my global variables because postman was lagging big time. I am going to try it again but with a new environment vs global.

Thanks,

Thanks Mike… great suggestion! I created an environment for it vs using global.

Thanks again!

Hey :wave:,

My example code was only based on your small response payload, if the actual response had 13k objects in the array, then it would have looped through and created that many variables.

Not sure what have happened with the values, the reference to the keys may have been different but I’m not sure of what the actual response structure.

1 Like

Thanks Danny. Appreciate your help! I altered it and this worked:

let responseData = JSON.parse(pm.response.json());

responseData.forEach((obj, index) => {
    pm.environment.set(`ReportName_${index}`, obj.ReportName);
    pm.environment.set(`ReportBlobUri_${index}`, obj.ReportBlobUri);
});

Doing _.each() and forEach is basically the same thing here, what’s different in the pm.response.json().

I wrongly assumed from your example, that it was a string response and not JSON. :grimacing:

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