# How to gather value from Json Body and make it a variable for chaining?

**URL:** <https://community.postman.com/t/how-to-gather-value-from-json-body-and-make-it-a-variable-for-chaining/10023>\
**Category:** Help Hub\
**Created:** [January 10, 2020, 11:21pm UTC](https://community.postman.com/t/how-to-gather-value-from-json-body-and-make-it-a-variable-for-chaining/10023 "2020-01-10T23:21:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BlkKnight01](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@BlkKnight01](https://community.postman.com/u/BlkKnight01)\
**Post date:** [January 10, 2020, 11:21pm UTC](https://community.postman.com/t/how-to-gather-value-from-json-body-and-make-it-a-variable-for-chaining/10023/1 "2020-01-10T23:21:17Z")

</div>

Get command = {{url}}/networkAsset/xxxxx/sites  
Initial GET returns JSON data containing all sites, I’m trying to discern how to capture the “id” data to use. Essentially I’d like to:  
a. Acquire the id-set as variable  
b. Use it in 3 other gets  
c. And then use the next id until there aren’t any more in the list

[  
{  
“self”: {  
“href”: “website”  
},  
“account”: {  
“href”: “/fooDomain/fooServlet/paths/12”  
},  
“id”: “ **xxxxxxxx-20b6-xxxxx-89ad-xxxxxxxxxx** ”,  
“type”: “Site”,  
“value”: “name of Site”,  
“assetInfo”: {  
“metadata”: {  
“tags”: ,  
}  
}  
},  
]

For each id I need to make 3 other calls

- {{url}}/networkAsset/xxxxxx/shoes?siteId
- {{url}}/networkAsset/xxxxxx/apples?siteId
- {{url}}/networkAsset/xxxxxx/oranges?siteId

How do capture and set each id as a variable to use in subsequent per site gets/calls?

---

<div class="post-metadata">

**Author:** ![john-paul](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/john-paul/32/919_2.png) [@john-paul](https://community.postman.com/u/john-paul)\
**Post date:** [January 11, 2020, 12:57am UTC](https://community.postman.com/t/how-to-gather-value-from-json-body-and-make-it-a-variable-for-chaining/10023/2 "2020-01-11T00:57:51Z")

</div>

Hi @BlkKnight01, welcome to the community!

So to confirm, you need to get/generate a list of IDs, then run each of those three requests for each ID?

I just built an example Collection that does this. 🙂

Go to the following docs page for a description of how it works, and click the **Run in Postman** button to get your own copy to play around with:

> **[Multi-request loop](https://documenter.getpostman.com/view/5293839/SWLiY5ab?version=latest)**
>
> This Collection gets a list of IDs, then loops through 3 POST requests that submit these IDs until there are none left.
> 
> The layout is as follows:
> 
> 
> \*\*GET ID list:\*\* https://postman-echo.com/get
> 
> Description:
> \- response contains an array of numbers...

Hope that helps! Let me know if you have questions.

---

<div class="post-metadata">

**Author:** ![BlkKnight01](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@BlkKnight01](https://community.postman.com/u/BlkKnight01)\
**Post date:** [January 13, 2020, 2:39pm UTC](https://community.postman.com/t/how-to-gather-value-from-json-body-and-make-it-a-variable-for-chaining/10023/3 "2020-01-13T14:39:31Z")

</div>

Greatly appreciate the reply john-paul. I ran the the initial get request for the id parse, and env variable set. The test error out:

 ![Json_Array_Error](https://us1.discourse-cdn.com/getpostman/original/3X/7/0/708b6becffd86d6b1e53e42040819f6717d6c719.jpeg)

---

<div class="post-metadata">

**Author:** ![john-paul](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/john-paul/32/919_2.png) [@john-paul](https://community.postman.com/u/john-paul)\
**Post date:** [January 14, 2020, 5:37pm UTC](https://community.postman.com/t/how-to-gather-value-from-json-body-and-make-it-a-variable-for-chaining/10023/4 "2020-01-14T17:37:35Z")

</div>

HI @BlkKnight01, do you have an environment selected in the app? I think I forgot to include one on the docs page.

---

<div class="post-metadata">

**Author:** ![BlkKnight01](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@BlkKnight01](https://community.postman.com/u/BlkKnight01)\
**Post date:** [January 15, 2020, 3:59pm UTC](https://community.postman.com/t/how-to-gather-value-from-json-body-and-make-it-a-variable-for-chaining/10023/5 "2020-01-15T15:59:37Z")

</div>

no … running without an environment but everything json.

one dataset in reply: not sure if where the id field is in the reply makes a difference.

[  
{  
“self”: {  
“href”: “[https://xxxxxxxxxxx/xxxxxxxx/xxxxxx/xxxx/953823aa-20b6-4be9-89ad-b9b37c92609b](https://xxxxxxxxxxx/xxxxxxxx/xxxxxx/xxxx/953823aa-20b6-4be9-89ad-b9b37c92609b)” --\> need string after the last “/”  
},  
“account”: {  
“href”: “/fooDomain/fooServlet/paths/12”  
},  
“id”: “953823aa-20b6-4be9-89ad-b9b37c92609b”, --\> need alpha/numeric string between quotes  
“type”: “xxxx”,  
“value”: “xxxxxxxxxxxxxxxxxxxxxx”,  
“assetInfo”: {  
“metadata”: {  
“tags”: ,  
“props”: {  
“address”: “xxxxxxxxxxxxxx”,  
“areaCount”: “3”,  
“name”: “xxxxxxxxxxxxxxxxxxxxxxxxxxxxx”,  
“organizationId”: “xxxxxx-xxxxxxxxxxxx-xxxxxxxxxx”  
}  
},  
“enabled”: true  
}  
},

---

<div class="post-metadata">

**Author:** ![john-paul](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/john-paul/32/919_2.png) [@john-paul](https://community.postman.com/u/john-paul)\
**Post date:** [January 15, 2020, 6:12pm UTC](https://community.postman.com/t/how-to-gather-value-from-json-body-and-make-it-a-variable-for-chaining/10023/6 "2020-01-15T18:12:40Z")

</div>

Got it. So, a couple things to note:

1. The code I wrote requires that an environment is selected to work (but you don’t have to put anything in it — everything that is needed is added by the code)
2. You’ll need to modify the first two line of code in your screenshot to suit the response you’re working with. In my Collection, the GET ID list request has a response that contains:  
`{args:{idArray:[1,2,3,4,5,...]}}`  
…so `idArrayString = pm.response.json().args.idArray` will work.

The basic principle here is to use Javascript to create a 1 dimensional array that contains all your ids (`[1,2,3,4,5,....]`), then store the stringified version of that array and its length in the environment.

Hope that helps!
