[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:
Parameter | Parameter Type | Required | Description |
---|---|---|---|
jsonObject | String | Yes | Specify the JSON object that needs to be validated. |
jsonSchema | String | Yes | 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 a Response against a JSON Schema:
Parameter | Parameter Type | Required | Description |
---|---|---|---|
response | ResponseObject | Yes | Specify the response object that needs to be validated. |
jsonSchema | String | Yes | Specify the JSON schema used to validate the response object. |
flowControl | FailureHandling | Optional | Specify failure handling schema to determine whether the execution should be allowed to continue or stop. |
Returns
Parameter Type | Description |
---|---|
Boolean |
|
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)