# How to grab variable from one API and add it to the endpoint of another api so it is called many times?

**URL:** <https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446>\
**Category:** Help Hub\
**Created:** [October 2, 2020, 9:58pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446 "2020-10-02T21:58:45Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 6, 2020, 5:12pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/21 "2020-10-06T17:12:43Z")

</div>

Got ya thank you so much much appreciate all the help you always give 🙂 i feel like i got a Postman Guru always helping out. Thanks to your help i have learned so much about Postman and javascript

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 13, 2020, 3:48pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/22 "2020-10-13T15:48:05Z")

</div>

Hi Allen,

had a question, for the same exact scenario and code that you wrote up front now how would i  
get a value which is already in an array?

so first it was to get the section title and sectionid but now i need to get the contenttitle and content id

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

the code i manipulated but no luck

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/4/448410fbc9de787b3c9958779d37ce34a97dce34.png)

also tried adding  
var id = jsonData[0][i].contentId;  
var modelString = jsonData[0][i].contentTitle;  
but not luck

---

<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:** [October 13, 2020, 4:05pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/23 "2020-10-13T16:05:08Z")

</div>

Do you want to iterate over all the items in the content array? Do you want to just look at the first one? what are you testing?

To get to the contents, you can do this:

```auto
for(let i = 0; i < jsonData.length; i++) {
  const result = jsonData[i];
  for(let contentIndex = 0; contentIndex < result.contents.length; i++) {
    const content = result.contents[contentIndex];
    const contentId = content.contentId;
    const contentTitle = content.contentTitle;
  }
}

```

I’ve probably mentioned this before, but it’s worth mentioning again. If you’re going to continue doing processing like this in Postman, I HIGHLY recommend you at least learn [what JSON is](https://www.w3schools.com/whatis/whatis_json.asp) and how to manipulate it. It will make your life so much easier.

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 13, 2020, 4:11pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/24 "2020-10-13T16:11:25Z")

</div>

Well for my test i want to do the same exact thing as i did above just only difference is its  
contentid and contenttitle

var jsonData = pm.response.json();

var resultCount = jsonData.length;

console.log(resultCount)

console.log(jsonData)

// Test arrays

var hasDelete = ;

var doesntHaveDelete = ;

// Loop through and set arrays with matching data

for (i = 0; i \< resultCount; i++) {

var id = jsonData[i].contentId;

var modelString = jsonData[i].contentTitle;

console.log(id)

console.log(modelString)

if (modelString.includes(“APIAUTOMATIONcontent”) || modelString.includes(“Testcontent”) || modelString.includes("{{POST-NewSlide}}") || modelString.includes(“stringcontent”)) {

```
hasDelete.push(id);

// (id) - this creates an integer (int)

// "announcementId": id, (creating object)

// "hasDelete": modelString.includes("Delete") || modelString.includes("Test")

// });

```

} else {

```
doesntHaveDelete.push(id)

// "announcementId": id

// });

```

}

}

// Check that each object in response contained keyword and length matches from test

pm.test(`Number of Announcement that has Delete or Test ready to be deleted = ${hasDelete.length} out of Total ${resultCount}`, function() {

console.log(hasDelete);

console.log(doesntHaveDelete);

pm.expect(hasDelete.length);

});

pm.collectionVariables.set(‘deletesections’, JSON.stringify(hasDelete));

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 13, 2020, 8:06pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/25 "2020-10-13T20:06:07Z")

</div>

> [@allenheltondev](#):
>
> ```auto
> for(let i = 0; i < jsonData.length; i++) {
> const result = jsonData[i];
> for(let contentIndex = 0; contentIndex < result.contents.length; i++) {
> const content = result.contents[contentIndex];
> const contentId = content.contentId;
> const contentTitle = content.contentTitle;
> }
> }
> 
> ```

I noticed everytime i run this Postman loads and than breaks … thanks i been trying to learn javascript and json but there are scenarios like nested loops which i am still new too

---

<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:** [October 13, 2020, 8:24pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/26 "2020-10-13T20:24:58Z")

</div>

I understand, looping can be tricky.

Especially when I send you code that has a bug in it 🙂  
On line 3, it should be incrementing `contentIndex`

```auto
for(let contentIndex = 0; contentIndex < result.contents.length; contentIndex++) {

```

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 13, 2020, 9:06pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/27 "2020-10-13T21:06:28Z")

</div>

> [@allenheltondev](#):
>
> ```auto
> for(let contentIndex = 0; contentIndex < result.contents.length; contentIndex++) {
> 
> ```

thanks , i was able to fix it and run it  
var jsonData = pm.response.json();

for(let i = 0; i \< jsonData.length; i++) {

const result = jsonData[i];

for(let x=0; x \< result.contents.length; x++) {

```
const content = result.contents[x];

const contentId = content.contentId;

const contentTitle = content.contentTitle;

console.log("Below is the contentId")

```

console.log(contentId)

console.log(“below is the content contentTitle”)

console.log(contentTitle)

console.log(“below is the result”)

console.log(result)

}

}

but , my question is how can i go with the same logic as the one that you had specified all the way on top?

so this code was working for the same api if i am to get the section id and sectiontitle since its not in the nested array , but if i do this in the content title and contentid it doesnt work…  
how can i tweak this :

for (i = 0; i \< resultCount; i++) {

var id = jsonData[i].contents[0].contentId;

var modelString = jsonData[i].contents[0].contentTitle;

console.log(id)

console.log(modelString)

if (modelString.includes(“APIAUTOMATIONcontent”) || modelString.includes(“Testcontent”) || modelString.includes(“{{POST-NewSlide}}”) || modelString.includes(“stringcontent”)) {

```
hasDelete.push(id);

// (id) - this creates an integer (int)

// "announcementId": id, (creating object)

// "hasDelete": modelString.includes("Delete") || modelString.includes("Test")

// });

```

} else {

```
doesntHaveDelete.push(id)

// "announcementId": id

// });

```

}

}

// Check that each object in response contained keyword and length matches from test

pm.test(`Number of Content that has APIAUTOMATIONcontent or Test ready to be deleted = ${hasDelete.length} out of Total ${resultCount}`, function() {

console.log(hasDelete);

console.log(doesntHaveDelete);

pm.expect(hasDelete.length);

});

pm.collectionVariables.set(‘deletesections’, JSON.stringify(hasDelete));

1

I changed the code in yellow highlight, but still doesnt work

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

also

sectionid 92 has content title APIAutomation and content id255  
I wanted to get all the content id and content title within an array with code above.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/8/b/8bcee86ed5f9751bf0664749dca710b2c2898dbd.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:** [October 14, 2020, 11:28am UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/28 "2020-10-14T11:28:45Z")

</div>

We might need to start a new thread for this question. This thread is getting real bogged down.

I still don’t understand what you want.

Do you want a list of all the `contentId`s and `contentTitle`s without being associated to their parent? Or do they need to still have context of the section.

What are you actually trying to test?

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 14, 2020, 4:07pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/29 "2020-10-14T16:07:29Z")

</div>

Thanks Allen, yeah your right this was getting crazy … just added the question in the link below

> [@How to grab variable from one API that is within a nested Array response body?](https://community.postman.com/t/how-to-grab-variable-from-one-api-that-is-within-a-nested-array-response-body/16758):
>
> How to grab the contentID and content title add it within an array and have it used in the URL of API 2 if i use the code below for section title and sectionid it is working because it is not in an nested array but for the contentid and contenttitle it is not working as it is in a nested array. API 1 test tab i have: for (i = 0; i \< resultCount; i++) { var id = jsonData[i].contents[0].contentId; var modelString = jsonData[i].contents[0].contentTitle; console.log(id) console.log(modelStrin…

its straight forward its working well for sectiontitle and sectionid but not contenttitle and contentid…

---

<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:** [October 14, 2020, 6:09pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/30 "2020-10-14T18:09:29Z")

</div>

for ContentId and Content Title, Let me know it works (other wise need to add another script

````auto
if (modelString.includes(“APIAUTOMATIONcontent”) || modelString.includes(“Testcontent”) || modelString.includes("{{POST-NewSlide}}") || modelString.includes(“stringcontent”)) ||modelString.includes(“contentId”)) || modelString.includes(“contentTitle”)) ```
````

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 16, 2020, 2:04pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/31 "2020-10-16T14:04:00Z")

</div>

Hi gpub,

thanks but its more about getting the contentid which is within the contents nested array  
and add it an array where i can use in the next request

this is the question over here if you could please analyze and help here …

> [@How to grab variable from one API that is within a nested Array response body?](https://community.postman.com/t/how-to-grab-variable-from-one-api-that-is-within-a-nested-array-response-body/16758):
>
> How to grab the contentID and content title add it within an array and have it used in the URL of API 2 if i use the code below for section title and sectionid it is working because it is not in an nested array but for the contentid and contenttitle it is not working as it is in a nested array. API 1 test tab i have: for (i = 0; i \< resultCount; i++) { var id = jsonData[i].contents[0].contentId; var modelString = jsonData[i].contents[0].contentTitle; console.log(id) console.log(modelStrin…

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [October 16, 2020, 5:09pm UTC](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446/32 "2020-10-16T17:09:45Z")

</div>

> [@How to grab variable from one API that is within a nested Array response body?](https://community.postman.com/t/how-to-grab-variable-from-one-api-that-is-within-a-nested-array-response-body/16758):
>
> How to grab the contentID and content title add it within an array and have it used in the URL of API 2 if i use the code below for section title and sectionid it is working because it is not in an nested array but for the contentid and contenttitle it is not working as it is in a nested array. API 1 test tab i have: for (i = 0; i \< resultCount; i++) { var id = jsonData[i].contents[0].contentId; var modelString = jsonData[i].contents[0].contentTitle; console.log(id) console.log(modelStrin…

[Previous page](https://community.postman.com/t/how-to-grab-variable-from-one-api-and-add-it-to-the-endpoint-of-another-api-so-it-is-called-many-times/16446.md?page=1)
