Validate JSON against Schema in Java

This tutorial shows how to validate JSON against Schema in Java. It presents a utility class – ValidationUtils.java that implements the validation logic. This tutorial uses the open source Jackson 2.x libraries and fge/json-schema-validator libraries hosted on GitHub.

Jackson, as of date, does not provide support for validating JSON data against a JSON schema. Hence, you cannot use Jackson for validation purposes. The fge JSON schema validator implements the missing validation support on top of Jackson.  It supports validation against both, JSON schema draft 3 and draft 4 compliant schemas.

The fge/json-schema-validator library provides a JsonSchema class that represents the JSON schema document. This class is not the same as the JsonSchema class provided by Jackson. To start with the validation process, all you need to do is to load your schema document into an instance of JsonSchema class provided by the schema validator. Similarly, load your JSON document that is to be validated, into an instance of JsonNode class provided by Jackson. Now, call the validate() method on the JsonSchema instance and pass in the JsonNode object. The validate() method returns a ProcessingReport instance that represents the result of the validation.

The isSuccess() method on the ProcessingReport object returns the status of the validation. If the validation fails, you can get the reasons for the failure by iterating the ProcessingReport object and retrieving the ProcessingMessage objects. The ProcessingMessage object represents an issue encountered during validation.

Let’s see an example. The schema.json file below shows a sample JSON schema document. The schema describes a product that has id, name and price as the required attributes and an optional array attribute called tags that should contain at least one value, if specified.

schema.json

The data.json file below shows a sample JSON data that conforms to the schema presented above.

data.json

The main method shown below shows the logic to validate JSON using the utility class presented further below in this tutorial. Note that we are loading the schema and the JSON documents from files. The utility class presents methods that can read the documents from in-memory Strings and remote URLs as well.

Main

 

ValidationUtils.java

Maven Dependencies

Output

 


 

29
Leave a Reply

avatar
300
14 Comment threads
15 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
11 Comment authors
QArlosMaruthishwetabhValentinSDrona Recent comment authors
  Subscribe  
newest oldest
Notify of
QArlos
Guest
QArlos

Congrats for your good job Drona!!!

shwetabh
Guest
shwetabh

Exception in thread “main” java.lang.NoClassDefFoundError: com.fasterxml.jackson.annotation.JsonMerge

I am getting this error.

Manoj
Guest
Manoj

Over eclipse, hovering the error, it shows “The method getJsonSchema(JsonNode) from the type JsonSchemaFactory refers to the missing type ProcessingException”

ValentinS
Guest
ValentinS

Hello im trying to use this to validate api json response vs schema in an automation project but i cant really figure out how i should do it.
I have the schema in a file.json and the response i can store it in JsonObject.
Could you please help with some tips how i should proceed next to validate?

Manoj
Guest
Manoj

Hi Drona,
I am also getting error in the below lines,
1)ProcessingReport report = jsonSchemaNode.validate(jsonNode);
2)return factory.getJsonSchema(jsonNode);
Can you please help me to solve this. Thanks in advance.

Rostislav
Guest
Rostislav

Hi Drona,
Sorry for my request, I found out that my schema wasn’t correct and it was cause of the problem. Validator works as expected.
Best regards,
Rostislav