Generate JSON schema from Java class

This tutorial shows you how to generate JSON schema from Java class.

We will use an open source library called JJSchema to do the job. To be able to generate the JSON schema properly, the Java class should have getters and setters defined for its members.

A JSON Schema is a JSON document that describes the structure of the JSON data. JSON Schemas are to JSON as XML Schemas are to XML. JSON Schema specification is currently under draft and the latest version is v4.

JJSchema is an open source library hosted on Github that can generate the latest draft v4 compliant JSON schemas. It uses the popular Jackson JSON processor Java library, internally. Jackson, itself, lacks the capability to generate JSON Schema from a Java class and hence libraries like JJSchema come as a great help when it comes generating JSON schemas.

The current release of JJSchema is 0.6 while version 1.0 is under development. This tutorial uses its 0.6 version.

Below is the maven dependency for JJSchema.

Maven dependency

Like XML schemas, JSON schemas includes a list of meta data about the schema fields. A partial list of meta data is given below.

  1. Name of the field
  2. Datatype of the field
  3. An optional description of the field
  4. Indicator on whether the field is mandatory or optional
  5. Optional constraints on number of occurrences and length of the value of the field

JJSchema provides an annotation @Attributes to specify the above meta data for the members of the Java class. In the example below, we have the Java class called Employee with fields for id, first name, last name, age, gender and multiple address lines.

Using the @Attributes annotation, we enforce the following constraints on the members of the Java class:

  1. first name and last name fields cannot be empty. Can be maximum 15 character long.
  2. address cannot be empty and can have up to and not exceeding 3 address lines. And each line cannot exceed 30 characters in length.
  3. gender field can only have MALE or FEMALE as its value

Take a close look at the annotations on each class member of the Employee class shown below and relate them with the discussion here.

Note: It is not absolutely necessary to annotate the Java class to be able to generate a JSON schema from it. You can pretty well do that without the annotations. But, the difference would be that without the annotations all fields would be generated as optional fields and you will not be able to specify and enforce constraints, if any.

Employee class

Now, after the Java class has been annotated, we are now ready to generate the JSON schema. Below is the main class that generates the schema.

Main class

 

Output

Below is a sample valid JSON data that conforms to the JSON schema generates above.

Validate JSON data

Below is a sample invalid JSON data that does not conform to the JSON schema generates above.

Invalid JSON data

 


 

2
Leave a Reply

avatar
300
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
rohit kashyap Recent comment authors
  Subscribe  
newest oldest
Notify of
rohit kashyap
Guest
rohit kashyap

this is not valid code….
1. gender property is not completely initialized.
2. after commenting above property still getting error as follows :
Exception in thread “main” java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

rohit kashyap
Guest
rohit kashyap

this is not valid code….
1. gender property is not comletely initilised.
2. after commenting above property still geting error as follows :
Exception in thread “main” java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType