# Log Response Issue

**URL:** <https://community.postman.com/t/log-response-issue/52361>\
**Category:** Help Hub\
**Tags:** variables, scripts\
**Created:** [September 25, 2023, 10:04pm UTC](https://community.postman.com/t/log-response-issue/52361 "2023-09-25T22:04:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitraqsr](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@mitraqsr](https://community.postman.com/u/mitraqsr)\
**Post date:** [September 25, 2023, 10:04pm UTC](https://community.postman.com/t/log-response-issue/52361/1 "2023-09-25T22:04:03Z")

</div>

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.

```auto
"[{\"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,

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 26, 2023, 9:11am UTC](https://community.postman.com/t/log-response-issue/52361/2 "2023-09-26T09:11:40Z")

</div>

Hey @mitraqsr 👋

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.

```auto
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);
});

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/2/c20b37d285660a9612aed297f6e8bae2a0c34ec2.png)

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.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 26, 2023, 10:03am UTC](https://community.postman.com/t/log-response-issue/52361/3 "2023-09-26T10:03:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mitraqsr](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@mitraqsr](https://community.postman.com/u/mitraqsr)\
**Post date:** [September 26, 2023, 5:35pm UTC](https://community.postman.com/t/log-response-issue/52361/4 "2023-09-26T17:35:36Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![mitraqsr](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@mitraqsr](https://community.postman.com/u/mitraqsr)\
**Post date:** [September 26, 2023, 5:36pm UTC](https://community.postman.com/t/log-response-issue/52361/5 "2023-09-26T17:36:06Z")

</div>

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

Thanks again!

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 26, 2023, 6:01pm UTC](https://community.postman.com/t/log-response-issue/52361/6 "2023-09-26T18:01:11Z")

</div>

Hey 👋,

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.

---

<div class="post-metadata">

**Author:** ![mitraqsr](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@mitraqsr](https://community.postman.com/u/mitraqsr)\
**Post date:** [September 26, 2023, 7:41pm UTC](https://community.postman.com/t/log-response-issue/52361/7 "2023-09-26T19:41:18Z")

</div>

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

```auto
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);
});

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 26, 2023, 9:45pm UTC](https://community.postman.com/t/log-response-issue/52361/8 "2023-09-26T21:45:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [September 29, 2023, 9:46pm UTC](https://community.postman.com/t/log-response-issue/52361/9 "2023-09-29T21:46:19Z")

</div>

Closed on September 29, 2023, 9:46pm UTC

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