Skip to main content

[WS] Validate JSON string against a schema

Description

Validate a JSON response body, request body, or string against a JSON schema. The JSON schema input can be a JSON string, URL, or file path.

Keyword name: validateJsonAgainstSchema

Parameters

Validate a JSON Object against a JSON Schema:
ParameterParameter TypeRequiredDescription
jsonObjectStringYesSpecify the JSON object that needs to be validated.
jsonSchemaStringYesSpecify the JSON schema used to validate the JSON object.
flowControlFailureHandlingOptionalSpecify failure handling schema to determine whether the execution should be allowed to continue or stop.
Validate a Response against a JSON Schema:
ParameterParameter TypeRequiredDescription
responseResponseObjectYesSpecify the response object that needs to be validated.
jsonSchemaStringYesSpecify the JSON schema used to validate the response object.
flowControlFailureHandlingOptionalSpecify failure handling schema to determine whether the execution should be allowed to continue or stop.

Returns

Parameter TypeDescription
Boolean
  • true: If the response passes the validation.
  • false: If the response does not pass the validation.

Example

  • Validate a JSON Object against a schema:
    import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

    String jsonPass =
    """
    {
    "\$id": "https://example.com/person.schema.json",
    "\$schema": "https://json-schema.org/draft/2020-12/schema",
    "title": "Person",
    "type": "object",
    "properties": {
    "firstName": {
    "type": "string",
    "description": "The person's first name."
    },
    "lastName": {
    "type": "string",
    "description": "The person's last name."
    },
    "age": {
    "description": "Age in years which must be equal to or greater than zero.",
    "type": "integer",
    "minimum": 0
    }
    }
    }
    """

    String jsonObject =
    """
    {
    "firstName": "White",
    "lastName": "Walter",
    "age": 52
    }

    """

    boolean successful = WS.validateJsonAgainstSchema(jsonObject,jsonPass)
  • Validate a Response against a schema:
    import com.kms.katalon.core.testobject.ResponseObject
    import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
    import com.kms.katalon.core.webservice.verification.WSResponseManager

    ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()

    String jsonPass =
    """
    {
    "\$id": "https://example.com/person.schema.json",
    "\$schema": "https://json-schema.org/draft/2020-12/schema",
    "title": "Person",
    "type": "object",
    "properties": {
    "firstName": {
    "type": "string",
    "description": "The person's first name."
    },
    "lastName": {
    "type": "string",
    "description": "The person's last name."
    },
    "age": {
    "description": "Age in years which must be equal to or greater than zero.",
    "type": "integer",
    "minimum": 0
    }
    }
    }
    """

    boolean successful = WS.validateJsonAgainstSchema(response,jsonPass)
Was this page helpful?