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