Created 11-06-2025 04:11 AM
I want to validate my JSON objects against an schema like this:
{
"type": "object",
"required": [
"apple",
"banana",
"orange"
],
"properties": {
"apple": {
"type": "object",
"required": [
"header",
"enableAlert"
],
"properties": {
"header": {
"type": "string"
},
"enableAlert": {
"type": [
"boolean",
"string"
],
"enum": [
true,
false,
"true",
"false"
]
}
},
"additionalProperties": true
},
"orange": {
"type": "object",
"required": [
"test1",
"test2"
],
"properties": {
"test1": {
"type": "string"
},
"test2": {
"type": "string"
}
}
},
"banana": {
"type": "boolean"
},
"additionalProperties": true
}
}The schema can change per flow file, so I want to derive it from flow file attribute and then use it as ${schema}. In the ValidateJSON 1.23.2 processor, the schema can't be derived from EL.
So I thought of using a script like this:
import org.apache.nifi.processor.io.StreamCallback
import java.nio.charset.StandardCharsets
import org.json.JSONObject
import org.everit.json.schema.loader.SchemaLoader
import org.everit.json.schema.ValidationException
import org.everit.json.schema.Schema
def flowFile = session.get()
if (!flowFile) return
def schemaStr = flowFile.getAttribute('schema')
if (!schemaStr) {
log.error("No 'schema' attribute found.")
session.transfer(flowFile, REL_FAILURE)
return
}
def isValid = true
def errorMsg = ""
flowFile = session.write(flowFile, { inputStream, outputStream ->
def content = new String(inputStream.bytes, StandardCharsets.UTF_8)
try {
def jsonData = new JSONObject(content)
def jsonSchema = new JSONObject(schemaStr)
def schema = SchemaLoader.load(jsonSchema)
schema.validate(jsonData)
} catch (ValidationException e) {
isValid = false
errorMsg = e.allMessages.join("; ")
} catch (Exception e) {
isValid = false
errorMsg = e.message
}
outputStream.write(content.bytes)
} as StreamCallback)
if (isValid) {
log.info("JSON validated successfully.")
session.transfer(flowFile, REL_SUCCESS)
} else {
log.error("JSON validation failed: ${errorMsg}")
flowFile = session.putAttribute(flowFile, 'validation.error', errorMsg)
session.transfer(flowFile, REL_FAILURE)
}This is my setup:
ls /root/integration_src/workarea/nifi/scripts/lib
json-20231013.jar org.everit.json.schema-1.14.3.jarI am getting this error now:
ExecuteScript[id=58d580dd-019a-1000-4e36-7b54d5c2d161] Processing failed: org.apache.nifi.processor.exception.ProcessException: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script5.groovy: 15: unable to resolve class org.everit.json.schema.loader.SchemaLoader @ line 15, column 1. import org.everit.json.schema.loader.SchemaLoader ^ Script5.groovy: 16: unable to resolve class org.everit.json.schema.ValidationException @ line 16, column 1. import org.everit.json.schema.ValidationException ^ Script5.groovy: 17: unable to resolve class org.everit.json.schema.Schema @ line 17, column 1. import org.everit.json.schema.Schema ^ 3 errors
Any idea on how to resolve the error?
Created 11-06-2025 09:19 AM
@phadkev
Have you considered using the ValidateRecord processor instead of the ValidateJson?
The ValidateRecord processor can be configured with a JSON reader. The Json readers can be configured to use "Schema Text". The Schema text property support expression language allowing you to extract the Schema you want to use from an inbound FlowFile attribute.
Please help our community grow. If you found any of the suggestions/solutions provided helped you with solving your issue or answering your question, please take a moment to login and click "Accept as Solution" on one or more of them that helped.
Thank you,
Matt