How to make the validations dynamic in API testing

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' }