Generate JSON Schema from Java Class

In this tutorial we are going to see about how to generate JSON Schema from Java Class using Jackson 1.x.

Jackson Maven Dependency

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

JSONSchemaGenerator.java

package com.ehowtonow.jackson;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig.Feature;
import org.codehaus.jackson.schema.JsonSchema;

public class JSONSchemaGenerator {

	public static void main(String[] args) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(Feature.WRITE_ENUMS_USING_TO_STRING, true);
		JsonSchema schema;
		try {
			schema = mapper.generateJsonSchema(Student.class);
			String jsonSchema = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
			System.out.println(jsonSchema);
		} catch (JsonMappingException e) {
			
			e.printStackTrace();
		} catch (JsonGenerationException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}

	    
	}

}

Student.java

package com.ehowtonow.jackson;

import java.util.Date;

public class Student {

	private String name;
	private String standard;
	private int age;
	private  Date date;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getStandard() {
		return standard;
	}
	public void setStandard(String standard) {
		this.standard = standard;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
		
}

JSON Schema Output

{
  "type" : "object",
  "properties" : {
    "name" : {
      "type" : "string"
    },
    "standard" : {
      "type" : "string"
    },
    "age" : {
      "type" : "integer"
    },
    "date" : {
      "type" : "number"
    }
  }
}

Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. How to convert JSON to Java Object using Jackson 1.x
  2. JSON Tree Model in Jackson
  3. How to Create Pretty Print JSON using Jackson
  4. Generate JSON Schema from Java Class
  5. Jackson Streaming API to Write JSON
  6. Convert Map to JSON using Jackson 1.x
  7. How to convert Object to JSON using Jackson 1.x
  8. JSON Streaming API to Read JSON
  9. Convert JSON to Map using Jackson

Be the first to comment

Leave a Reply