57

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"
      }
}
Lii
  • 11,553
  • 8
  • 64
  • 88
user3587174
  • 661
  • 3
  • 8
  • 13
  • IS this what you are looking for? http://stackoverflow.com/questions/9593409/convert-pojo-to-json – eri0o Oct 05 '14 at 05:01
  • 1
    No, that converts pojo to json object. I am looking for generating JSON schema as meta [information about the input form fields mapped to pojo fields like datatype, whether it is required or not, etc., ] to the end users). – user3587174 Oct 05 '14 at 14:23
  • Here is an online site that will produce json schema from json: http://www.jsonschema.net/ – DwB Oct 06 '14 at 19:07
  • possible duplicate of [Tool to generate JSON schema from JSON data](http://stackoverflow.com/questions/7341537/tool-to-generate-json-schema-from-json-data) – DwB Oct 06 '14 at 19:09
  • Actually, I am not looking for any tools. I need an api that can have helper classes or annotations to describe the behavior of fields in a pojo. E.g. i recently found today that jackson 2.4.1 has new annotation @JsonPropertyDescription to add the description to the field in a pojo; [link]http://stackoverflow.com/questions/24515917/generating-jsonschema-from-pojo-how-do-i-add-description-automatically. Is there a possible way to achieve the example in my post through reflection? – user3587174 Oct 06 '14 at 19:32
  • I noticed Jackson doesn't have this feature yet, so i chose to build metadata using reflection on the pojo. – user3587174 Oct 09 '14 at 15:35
  • Jackson does have this functionality via JSON Schema module: https://github.com/FasterXML/jackson-module-jsonSchema – StaxMan Oct 23 '14 at 20:59

4 Answers4

15

EDIT: as pointed out by commenters, module is being deprecated, not maintained. So, Caveat Emptor etc


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.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • The module you specify doesn't work if you have complex objects containing enum types that need to stay enums. See my answer below for a better tool (Still Jackson, but jackson-mapper.) The instructions at your link result in Enums being rendered as simple strings in the schema – StormeHawke Jan 13 '15 at 01:51
  • Yes, I saw a bug report. I hope we get more contributors for the module -- it's external contribution, quite widely used, but no dedicated owner at this point. – StaxMan Jan 13 '15 at 21:55
  • 6
    Downvoted... not because it was a bad answer, just because it's a bad answer for *today*. This library is not being maintained anymore. – Robert Aug 15 '21 at 17:00
  • 1
    Unmaintained and does not support draft 4, 6, 7 https://github.com/FasterXML/jackson-module-jsonSchema/issues/141 – kane Mar 10 '23 at 06:03
13

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.

Edgar Domingues
  • 930
  • 1
  • 8
  • 17
2

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.

Drona
  • 6,886
  • 1
  • 29
  • 35
  • https://github.com/mbknor/mbknor-jackson-jsonSchema can generate Draft 4 schema based on Jackson annotations – mbknor Oct 28 '16 at 17:09
1
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;
    }
P Rajesh
  • 326
  • 1
  • 2
  • 11