# How to set global variables with set of value from json response?

**URL:** <https://community.postman.com/t/how-to-set-global-variables-with-set-of-value-from-json-response/25184>\
**Category:** Help Hub\
**Created:** [June 24, 2021, 6:55pm UTC](https://community.postman.com/t/how-to-set-global-variables-with-set-of-value-from-json-response/25184 "2021-06-24T18:55:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ps198478320](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@ps198478320](https://community.postman.com/u/ps198478320)\
**Post date:** [June 24, 2021, 6:55pm UTC](https://community.postman.com/t/how-to-set-global-variables-with-set-of-value-from-json-response/25184/1 "2021-06-24T18:55:35Z")

</div>

Hi,  
my request has this response:

> [  
> {  
> “Site”: “[google.com](http://google.com)”,  
> “external”: false,  
> “active”: true,  
> “black”: false,  
> “AVR”: false  
> },  
> {  
> “Site”: “google.ca”,  
> “external”: false,  
> “active”: true,  
> “black”: false,  
> “AVR”: false  
> },  
> {  
> “Site”: “[yulu.com](http://yulu.com)”,  
> “external”: true,  
> “active”: false,  
> “black”: true,  
> “AVR”: true  
> }  
> ]

I want to set global variable with each set of data like:  
var1 = “Site”: “[google.com](http://google.com)”,  
“external”: false,  
“active”: true,  
“black”: false,  
“AVR”: false

var2 = “Site”: “google.ca”,  
“external”: false,  
“active”: true,  
“black”: false,  
“AVR”: false

var3 = “Site”: “[yulu.com](http://yulu.com)”,  
“external”: true,  
“active”: false,  
“black”: true,  
“AVR”: true

I am able to set each value in one var like for example:  
var1= “Site”: “[google.com](http://google.com)”,“Site”: “google.ca”,“Site”: “[yulu.com](http://yulu.com)”  
by using this code:

```
> var jsonData = JSON.parse(responseBody);

```

> ```
> var jsonNamesData = jsonData;
> console.log(jsonNamesData);
> var parsedData = "";
> for(var i=0;i<jsonNamesData.length;i++){
> parsedData = parsedData +"\"Site\" : \"" +jsonNamesData[i].Site+"\", ";
> console.log("\"Site\" : \"" +jsonNamesData[i].Site+"\"");
> }
> pm.globals.set("Site", parsedData);
> 
> ```

Thank you for your help.

---

<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:** [June 24, 2021, 7:13pm UTC](https://community.postman.com/t/how-to-set-global-variables-with-set-of-value-from-json-response/25184/2 "2021-06-24T19:13:06Z")

</div>

Hey @ps198478320

You could probably achieve this with something like:

```auto
_.each(pm.response.json(), (item, index) => { 
    pm.globals.set(`var${index + 1}`, JSON.stringify(item))
})

```

---

<div class="post-metadata">

**Author:** ![ps198478320](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@ps198478320](https://community.postman.com/u/ps198478320)\
**Post date:** [June 24, 2021, 7:54pm UTC](https://community.postman.com/t/how-to-set-global-variables-with-set-of-value-from-json-response/25184/3 "2021-06-24T19:54:49Z")

</div>

Hi @danny-dainton ,  
you solution works but it stores only the last set of value:

“Site”: “[yulu.com](http://yulu.com/)”,  
“external”: true,  
“active”: false,  
“black”: true,  
“AVR”: true

i’m looking at how to do for all set of value…

---

<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:** [June 24, 2021, 8:07pm UTC](https://community.postman.com/t/how-to-set-global-variables-with-set-of-value-from-json-response/25184/4 "2021-06-24T20:07:57Z")

</div>

It should be looping through the whole array and setting each of the objects as a new variables.

Could you share an image of the app and how you have the code?

Did you use the code as I wrote it?

What ever the name you have chosen for the variable, this part is important `${index + 1}` as it gives a unique name for the variable.

Without this, you’re going to overwrite the last stored variable and probably end up with the last one.

---

<div class="post-metadata">

**Author:** ![ps198478320](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@ps198478320](https://community.postman.com/u/ps198478320)\
**Post date:** [June 24, 2021, 8:16pm UTC](https://community.postman.com/t/how-to-set-global-variables-with-set-of-value-from-json-response/25184/5 "2021-06-24T20:16:39Z")

</div>

Yes i use exactly your code:

```
_.each(pm.response.json(), (item, index) => { 
pm.globals.set('item', JSON.stringify(item))

```

})

 ![item](https://us1.discourse-cdn.com/getpostman/original/2X/3/3479f37d3d0863ed9c46a32340c821d6cc12ed22.png)

---

<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:** [June 24, 2021, 8:18pm UTC](https://community.postman.com/t/how-to-set-global-variables-with-set-of-value-from-json-response/25184/6 "2021-06-24T20:18:24Z")

</div>

> [@danny-dainton](#):
>
> What ever the name you have chosen for the variable, this part is important `${index + 1}` as it gives a unique name for the variable.
> 
> Without this, you’re going to overwrite the last stored variable and probably end up with the last one.

It’s not quite as I wrote it 😁

---

<div class="post-metadata">

**Author:** ![ps198478320](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@ps198478320](https://community.postman.com/u/ps198478320)\
**Post date:** [June 24, 2021, 8:21pm UTC](https://community.postman.com/t/how-to-set-global-variables-with-set-of-value-from-json-response/25184/7 "2021-06-24T20:21:29Z")

</div>

You are right, i made a mistake, it works now, it goes to all value and stores in variable.  
Thank you very much.

---

<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:** [December 12, 2024, 12:57pm UTC](https://community.postman.com/t/how-to-set-global-variables-with-set-of-value-from-json-response/25184/8 "2024-12-12T12:57:14Z")

</div>

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