Here is how i did it. Is there any better way
Custom Keyword
package com.api.helpers
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.util.KeywordUtil
import groovy.json.JsonSlurper
public class ResponseVerifier {
/**
* Verifies if a specific value exists within a JSON array attribute.
* @param response The ResponseObject returned from your API call.
* @param jsonPathElement The dot-notation path to the element containing the array (e.g., 'orders.status')
* @param expectedValue The value you are searching for (e.g., 'Invoiced')
* @return boolean true if found, false otherwise
*/
@Keyword
def static boolean verifyValueExistsInArray(ResponseObject response, String jsonPathElement, String expectedValue) {
try {
// 1. Parse the raw response text into a Groovy object
JsonSlurper slurper = new JsonSlurper()
def jsonResponseBody = slurper.parseText(response.getResponseBodyContent())
// 2. Use Groovy's GPath (similar to JsonPath) to collect the values
// evaluation safely navigates nested maps/lists
def actualValues = jsonResponseBody."${jsonPathElement}"
// 3. Check if the value exists in the extracted collection
if (actualValues != null && actualValues.contains(expectedValue)) {
KeywordUtil.logInfo("Success! Found '${expectedValue}' in the array.")
return true
} else {
KeywordUtil.markFailed("Failure! '${expectedValue}' was not found in the array.")
return false
}
} catch (Exception e) {
KeywordUtil.markFailed("An error occurred while parsing the JSON: " + e.getMessage())
return false
}
}
}