# Capture Json guid value and save in different variable for each iteration

**URL:** <https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902>\
**Category:** Help Hub\
**Tags:** variables, collections, newman-project, collection-runner, tests\
**Created:** [September 15, 2020, 8:06pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902 "2020-09-15T20:06:32Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 15, 2020, 8:06pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/1 "2020-09-15T20:06:32Z")

</div>

> **Archived topic — this may be out of date.**  
> Newman has been replaced by the **[Postman CLI](https://learning.postman.com/docs/postman-cli/postman-cli-overview/)**, Postman’s official, signed command-line tool. It runs collections as Newman did, and also lints API specs, runs mocks and monitors, and syncs local files with Postman from your terminal, CI/CD pipeline or coding agent.  
> Working with an AI coding agent? The [Postman plugin](https://github.com/postmanlabs/postman-plugin) lets agents in Claude Code, Cursor, Codex and others design, mock, test and ship APIs with Postman.  
> For current help, see the [Postman docs](https://learning.postman.com/docs/), try the free [Postman tools](https://www.postman.com/tools) (no account needed), or join us on [Discord](https://discord.gg/58aNNsRBGR).

I know i can add same Json request to capture value of itemlist again and save it diff variables.

Is there another way to run same request in multiple iteration, instead overwriting same Env variable it should save itemlist (since its different each time) with different variables like for var2, var3 so on..

Run same request in iteration and save itemid[i] into Variable\_1,Variable\_2 etc for Itemid1, itemid2…so on respectively

for e.g like For 5 iteration in can save 5 env variable with same run via newman on postmanrunner?

```auto
Token Generated
ItemId -1(captured itemid and saved it in Variable_1) 
Saved it in variable_1

```

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 17, 2020, 11:49am UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/2 "2020-09-17T11:49:40Z")

</div>

If I understand you correctly, you want to run the same request multiple times, and grab a unique value and save it to a new variable each time the request is run?

You could do something like this:

```auto
let iterationCount = pm.collectionVariables.get('iterationCount');
if(iterationCount) {
  iterationCount = Number(iterationCount);
}
else {
  iterationCount = 1;
}

const jsonData = pm.response.json();
const value = jsonData.value; // this would be whatever your property you want

let values = pm.collectionVariables.get('values');
if(values) {
  values = JSON.parse(values);
}
else {
  values = [];
}

values.push(value);
pm.collectionVariables.set('values', JSON.stringify(values));

iterationCount++;

if(iterationCount <= 5) { // This would be your threshold for how many times to loop
  pm.collectionVariables.set('iterationCount', iterationCount);
  postman.setNextRequest('Name of this request');
} 
else {
  pm.collectionVariables.unset('iterationCount');
}

```

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 17, 2020, 2:47pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/3 "2020-09-17T14:47:12Z")

</div>

> [@allenheltondev](#):
>
> ```auto
> let iterationCount = pm.collectionVariables.get('iterationCount');
> if(iterationCount) {
> iterationCount = Number(iterationCount);
> }
> else {
> iterationCount = 1;
> }
> 
> const jsonData = pm.response.json();
> const value = jsonData. **itemid** ; // this would be whatever your property you want
> 
> let values = pm.collectionVariables.get('values');
> if(values) {
> values = JSON.parse(values);
> }
> else {
> values = [];
> }
> 
> values.push(value);
> pm.collectionVariables.set('values', JSON.stringify(values));
> 
> iterationCount++;
> 
> if(iterationCount <= 5) { // This would be your threshold for how many times to loop
> pm.collectionVariables.set('iterationCount', iterationCount);
> postman.setNextRequest(' **GenerateItemid**');
> } 
> else {
> pm.collectionVariables.unset('iterationCount');
> }
> 
> ```

Yes @allenheltondev, i am looking the same thing. Let me run and comeback to you.  
i have updated the value and request name. There is too much of “value” text used 😛 getting confused

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 17, 2020, 2:55pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/4 "2020-09-17T14:55:12Z")

</div>

@allenheltondev seems like it did run 5 times but getting error on declaration part. Also variables were not added in env. i used this code in test script.  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/0/7067212cb6c259d9c04005e81a4c3fe952a2bae2.png)

After bit cleaning, i am not getting ReferenceError: Cannot access ‘jsonData’ before initialization

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/5/05c9c5f5ca3d5a37bea8f9b8178c6c13a9f59a8c.png)

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 17, 2020, 3:08pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/5 "2020-09-17T15:08:30Z")

</div>

Can you post the updated script?

I had the variables set to collection variables, not environment variables. You’ll have to change that in the javascript if you want it to be set to an environment variable.

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 17, 2020, 3:15pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/6 "2020-09-17T15:15:38Z")

</div>

environment changed to from collection

```auto
let iterationCount = pm.environment.get('iterationCount');
if(iterationCount) {
  iterationCount = Number(iterationCount);
}
else {
  iterationCount = 1;
}
const jsonData = pm.response.json();
//const jsonData = JSON.parse(responseBody);
const value = jsonData.itemid; // updated with required path
let values = pm.environment.get('values');
if(values) {
  values = JSON.parse(values);
}
else {
  values = [];
}
values.push(value);
pm.environment.set('values', JSON.stringify(values));
iterationCount++;
if(iterationCount <= 5) { // This would be your threshold for how many times to loop
  pm.environment.set('iterationCount', iterationCount);
  postman.setNextRequest('GenerateItem');
} 
else {
  pm.environment.unset('iterationCount');
}

```

updated only unique itemid path from response and the environment getting only 1 value that too i think of 2nd iteration not like “112503877”

![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/5/a5b09c98b9ebadfb5fb8dfc197ecb018f658acdd.png)

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 17, 2020, 4:00pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/7 "2020-09-17T16:00:00Z")

</div>

@allenheltondev. Thank but the requirement is iteration = 5 and values are saved in variable like: i used these item id separately must not used same itemid again  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/d/6d2ba963771bfe24fcfc94eb7a02b96495eb795d.png)

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 17, 2020, 4:15pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/8 "2020-09-17T16:15:35Z")

</div>

No problem, you can just create the variables dynamically:

```auto
let iterationCount = pm.environment.get('iterationCount');
if(iterationCount) {
  iterationCount = Number(iterationCount);
}
else {
  iterationCount = 1;
}
const jsonData = pm.response.json();
pm.environment.set(`item_${iterationCount+1}`, jsonData.itemid);
iterationCount++;
if(iterationCount <= 5) { // This would be your threshold for how many times to loop
  pm.environment.set('iterationCount', iterationCount);
  postman.setNextRequest('GenerateItem');
} 
else {
  pm.environment.unset('iterationCount');
}

```

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 17, 2020, 4:46pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/9 "2020-09-17T16:46:53Z")

</div>

yeah pretty close, missed the 3rd

![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/1/0173e1fe214db1099c92d6f5d8882572715d8092.png)

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 17, 2020, 4:59pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/10 "2020-09-17T16:59:24Z")

</div>

Are you sure that value exists in the API response?  
I’d recommend adding a test in there.

```auto
let iterationCount = pm.environment.get('iterationCount');
if(iterationCount) {
  iterationCount = Number(iterationCount);
}
else {
  iterationCount = 1;
}
const jsonData = pm.response.json();
pm.test('Response has itemId', function(){
  pm.expect(jsonData).to.have.property('itemid');
});

pm.environment.set(`item_${iterationCount+1}`, jsonData.itemid);
iterationCount++;
if(iterationCount <= 5) { // This would be your threshold for how many times to loop
  pm.environment.set('iterationCount', iterationCount);
  postman.setNextRequest('GenerateItem');
} 
else {
  pm.environment.unset('iterationCount');
}

```

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 17, 2020, 5:29pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/11 "2020-09-17T17:29:21Z")

</div>

yes Allen, item dies exists in response. as guided Item test is pass and returning. below  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/b/9b198c83df5b7a1e2b005c8456906a0713732ffd.png)

This time it generated only 1 itemid

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 17, 2020, 6:01pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/12 "2020-09-17T18:01:46Z")

</div>

Some problem in `postman.setNextRequest('generateitem');`  
`if(iterationCount <= 8)`  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/e/4/e4cd55a12d1d4994bcab342c7f86c3790d5472e8.png)  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/7/57300843aa0c6cdab3927f0146f3a5aac6e4f65a.png)  
have to manually send same request.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 17, 2020, 8:20pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/13 "2020-09-17T20:20:17Z")

</div>

I want to make sure you’re testing this by running it through the collection runner.

I just copied this exact script into my workspace and ran it through the runner without issues. it iterated 5 times and created 5 environment variables.

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 17, 2020, 8:56pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/14 "2020-09-17T20:56:02Z")

</div>

Opps,😛 Sorry i ran it seperately. but when i ran it with collection it only make 3 env variables. There was some problem in my collection. let me move this and ran again…hopefully it will work.

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 18, 2020, 3:04pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/15 "2020-09-18T15:04:53Z")

</div>

Just one thing @allenheltondev, will same work with newman too ? i know its silly 😃

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 18, 2020, 3:25pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/16 "2020-09-18T15:25:33Z")

</div>

Yes it will. It should work as long as you aren’t sending it as a one-off single request.

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 18, 2020, 3:26pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/17 "2020-09-18T15:26:43Z")

</div>

No i won’t :D, it will be collection. Thanks and Sorry i made you double check the code 😛

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 18, 2020, 3:30pm UTC](https://community.postman.com/t/capture-json-guid-value-and-save-in-different-variable-for-each-iteration/15902/18 "2020-09-18T15:30:22Z")

</div>

Don’t worry about it, I’m happy to help.
