Skip to main content

[WS] Validate JSON string against a schema (PoC)

Important:
  • This Proof of Concept (PoC) is not ready for production use. We recommend using this PoC for evaluation purposes only.
  • For version 8.3.1.alpha, this keyword name is WS.validateJsonSchema.
  • For version 8.4.0.beta onwards, this keyword name is WS.validateJsonAgainstSchema

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.

Parameters

Validate a JSON Object with a JSON Schema

Parameter Parameter Type Mandatory Description
jsonObject String Required Specify the JSON object that needs to be validated
jsonSchema String Required Specify the JSON schema used to validate the JSON object.
flowControl FailureHandling Optional Specify failure handling schema to determine whether the execution should be allowed to continue or stop.

Validate the Response with 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.validateJsonSchema(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.validateJsonSchema(response,jsonPass)