Checking that an ID is appearing or not in my Response Body

Hello,

I am using an API to add and delete movies from a list, I’m using postman collection variables to take the id of the new movie I added so that I can use it in some tests. Here is an example of what the list of movies looks like:

[
{

    "actors": "Kevin Zegers, Rip Torn",

    "category": "Popular",

    "director": "Peter Hewitt",

    "genres": "Action, Adventure, Family, Sci-Fi",

    "id": 1457,

    "imdb": "https://www.imdb.com/title/tt0383060/",

    "language": "English",

    "minutes": 83,

    "rating": 4,

    "stock": 3,

    "title": "Zoom",

    "year": 2006

},

{

    "actors": "Orlando Bloom, Tanya van Graan",

    "category": "Regular",

    "director": "Jérôme Salle",

    "genres": "Crime, Drama, Thriller",

    "id": 2516,

    "imdb": "https://www.imdb.com/title/tt2249221/",

    "language": "English",

    "minutes": 110,

    "rating": 3,

    "stock": 2,

    "title": "Zulu",

    "year": 2013

}

]

Using the last entry with an id of 2516 as an example, I want the test to either log that the movie exists in the list based on its id or if it doesn’t exist. So far I have tried this but it doesn’t quite work:

if(pm.response.text().includes(pm.collectionVariables.get(“idData”)) === true) {

console.log("Movie exists within the list");

}

else {

console.log("Movie no longer exists in the list")

}

I’m sure I’m doing something wrong or missing something. There’s probably a better way of doing it. I appreciate any help.

@AdrianChirion Welcome to the Community :partying_face:

Can you please check the similar post discussed here?

Thanks for the response!

I tried applying that solution but I kept getting the error ‘TypeError: Cannot read property ‘0’ of undefined’

You make sure to give your path correctly based on your response :blush:

This post might help you to locate your path using https://jsonpathfinder.com/

I managed to get it working! Thank you so much!

1 Like