Validate JSON against Schema in Java

This tutorial shows how to validate JSON against Schema in Java. It presents a utility class – 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.


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


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.


Maven Dependencies




0 0 votes
Article Rating
Notify of
Oldest Most Voted
Inline Feedbacks
View all comments
3 years ago

Thanks Drona !

Last edited 3 years ago by sadsridh
6 years ago

Congrats for your good job Drona!!!

6 years ago

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

I am getting this error.

7 years ago

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

Reply to  Manoj
7 years ago

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?

7 years ago

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.

Previous article

Jackson: Ignore null and empty fields