How to grab variable from one API that is within a nested Array response body?

314 views Asked by At

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. enter image description here

In the 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(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

There are 1 answers

1
Christian Baumann On BEST ANSWER

Like you're iterating over each section in your response body, you also need to iterate over the contents array, you can do it like below:

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

    contentCount = jsonData[i].contents.length;
    for (j = 0; j < contentCount; j++) {
        let content = jsonData[i].contents[j];
        console.log(content.contentId);
        console.log(content.sectionId);
        console.log(content.contentTitle);
        console.log(content.pdfLink);
        console.log(content.videoLink);
        console.log(content.sortOrder);
    }
}