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

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

1 Like

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

the code i manipulated but no luck

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

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:

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 and how to manipulate it. It will make your life so much easier.

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));

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

I understand, looping can be tricky.

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

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

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.

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 contentIds and contentTitles without being associated to their parent? Or do they need to still have context of the section.

What are you actually trying to test?

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

its straight forward its working well for sectiontitle and sectionid but not contenttitle and contentidā€¦

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

if (modelString.includes(ā€œAPIAUTOMATIONcontentā€) || modelString.includes(ā€œTestcontentā€) || modelString.includes("{{POST-NewSlide}}") || modelString.includes(ā€œstringcontentā€)) ||modelString.includes(ā€œcontentIdā€)) || modelString.includes(ā€œcontentTitleā€)) ```

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 ā€¦