How to make the validations dynamic in API testing

We are doing Rest API Testing through Katalon Studio and we are trying to validate product ids from the response. The json path that we got for product validation is “listPackage[1].cart[2].id” for which the index is static . But the response varies from time to time and the position of the cart id will not be the we need to give the validations dynamically (irresepective of the index) such that if the response varies the script should not fail

Currently the code that i have written is "Response, ‘listPackage[0].cart[0].id’, ‘abc’ ". And the property that we are using is Verify Element Property Value(Response, ‘listPackage[0].cart[0].id’, ‘abc’). Now i want to make this dynamic such that if the indexing gets changed the script should not fail.

could you please give a solution for the same?

Your problem looks similar to this, which requires some external libraries :

And also GPath Query might be easier, as it does not require additional libraries. See:

You should learn what the find method in the sample code does:

def theNode = json.results[0].address_components
        .find { it.types[0] == 'postal_code' }