How to verify a specific value in a huge JSON array?

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

@qurzunta.kaab I think you can try this,

`@Keyword
static boolean verifyValueExistsInArray(
        ResponseObject response,
        String jsonPathElement,
        String expectedValue) {
    try {
        def json = new JsonSlurper().parseText(response.getResponseBodyContent())
        def actualValues = jsonPathElement.split('\\.').inject(json) { node, key ->
            node instanceof List ? node.collect { it[key] }.flatten() : node?[key]
        }

        if (actualValues == null) {
            KeywordUtil.markFailed("Path '${jsonPathElement}' resolved to null or does not exist.")
            return false
        }

        def valueList = actualValues instanceof List ? actualValues : [actualValues]

        if (valueList.contains(expectedValue)) {
            KeywordUtil.logInfo("Found '${expectedValue}' at '${jsonPathElement}'.")
            return true
        } else {
            KeywordUtil.markFailed(
                "'${expectedValue}' not found at '${jsonPathElement}'. " +
                "Actual values: ${valueList.take(10)}"  // cap output for huge arrays
            )
            return false
        }
    } catch (Exception e) {
        KeywordUtil.markFailed("JSON parsing/traversal error: ${e.getMessage()}")
        return false
    }
}

@qurzunta.kaab you can use Katalon’s built-in WS.verifyElementsCount or simply parse and use Groovy’s native collection methods inline in your test script:

`import groovy.json.JsonSlurper

def response = WS.sendRequest(findTestObject('your/api'))
def json = new JsonSlurper().parseText(response.getResponseBodyContent())

// Flat array
assert json.orders.status.contains('Invoiced')