Closed ghost closed 5 years ago
Hi @nik0la-vr The following method returns whether the given JSON is valid or not as a boolean value. Both the JSON and the JSON schema are specified as string type.
private static JsonValidationService service = JsonValidationService.newInstance();
public static boolean validateJson(String schemaAsString, String jsonAsString) {
JsonSchema schema = service.readSchema(new StringReader(schemaAsString));
List<Problem> problems = new ArrayList<>();
ProblemHandler handler = ProblemHandler.collectingTo(problems);
try (JsonReader reader = service.createReader(new StringReader(jsonAsString), schema, handler)) {
reader.readValue();
}
return problems.isEmpty();
}
In Java, you can pass an instance of StringReader
to any method accepting Reader
.
I believe you can easily modify the code above to return the list of problems found in the validation.
Thanks.
Hi.
How to get boolean as a result, true or false if json is valid or not?
How to return list of problems from my method where validation is performed?