Generate JSON schema from Java class

JsonJacksonJsonschemaFasterxmlJackson Modules

Json Problem Overview


I have a POJO class:

public class Stock {
 int id;
 String name;
 Date date;
}

Are there any annotations or development framework/API that can convert POJO to JSON schema like below:

{"id":
      {				
        "type" : "int"
      },
"name":{   
        "type" : "string"
       }
"date":{
        "type" : "Date"
      }
}

And also I can expand the schema to add information like "Required" : "Yes", description for each field, etc., by specifying some annotations or configurations on POJO and can generate JSON Schema like below:

{"id":
      {				
        "type" : "int",
        "Required" : "Yes",
        "format" : "id must not be greater than 99999",
        "description" : "id of the stock"
      },
"name":{   
        "type" : "string",
        "Required" : "Yes",
        "format" : "name must not be empty and must be 15-30 characters length",
        "description" : "name of the stock"
       }
"date":{
        "type" : "Date",
        "Required" : "Yes",
        "format" : "must be in EST format",
        "description" : "filing date of the stock"
      }
}

Json Solutions


Solution 1 - Json

One such tool is Jackson JSON Schema module:

https://github.com/FasterXML/jackson-module-jsonSchema

which uses Jackson databind's POJO introspection to traverse POJO properties, taking into account Jackson annotations, and produces a JSON Schema object, which may then be serialized as JSON or used for other purposes.

Solution 2 - Json

Java JSON Schema Generator: https://github.com/victools/jsonschema-generator

Creates JSON Schema (Draft 6, Draft 7 or Draft 2019-09) from Java classes using Jackson.

Solution 3 - Json

Use JJschema. It can generate draft 4 compliant JSON schemas. Refer this post http://wilddiary.com/generate-json-schema-from-java-class/ for details.

Though Jackson Json Schema module can too generate schema but it can, as of today, only generate draft 3 compliant schemas only.

Solution 4 - Json

public static String getJsonSchema(Class clazz) throws IOException {
		 Field[] fields = clazz.getDeclaredFields();
		 List<Map<String,String>> map=new ArrayList<Map<String,String>>();
		 for (Field field : fields) {
			 HashMap<String, String> objMap=new  HashMap<String, String>();
			 objMap.put("name", field.getName());
			 objMap.put("type", field.getType().getSimpleName());
			 objMap.put("format", "");
			 map.add(objMap);
		 }
		 ObjectMapper mapper = new ObjectMapper();
		 String json = mapper.writeValueAsString(map);
        
       return json;
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3587174View Question on Stackoverflow
Solution 1 - JsonStaxManView Answer on Stackoverflow
Solution 2 - JsonEdgar DominguesView Answer on Stackoverflow
Solution 3 - JsonDronaView Answer on Stackoverflow
Solution 4 - JsonP RajeshView Answer on Stackoverflow