Skip to main content

[WS] Validate JSON string against a schema

Requirements​

  • Katalon Studio version 8.4.0 onwards.

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: WS.validateJsonAgainstSchema

Parameters​

Validate a JSON Object against a JSON Schema​

ParameterParameter TypeMandatoryDescription
jsonObjectStringRequiredSpecify the JSON object that needs to be validated
jsonSchemaStringRequiredSpecify 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 the Response against a JSON Schema​

ParameterParameter TypeMandatoryDescription
responseResponseObjectRequiredSpecify the response object that needs to be validated
jsonSchemaStringRequiredSpecify 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: the response passes the validation.
  • false: the response doesn't pass the validation.

Note:
  • If Katalon Studio cannot find the schema file or the response doesn't pass the validation, throw: StepFailedException.

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)