How to generate json schema without using POJO classes in JAVA?

923 views
Skip to first unread message

Anup Seth

unread,
Aug 30, 2017, 9:08:03 AM8/30/17
to JSON Schema
Currently I am able to generate json schema without POJO as per json schema draft 3, this is causing some issue as it is not according to the latest standards.

Problem is as per old specification the value of required can be true if the property is compulsory. But now the required can be array of property names which are required.

code:

        ObjectMapper mapper = new ObjectMapper();
        ObjectSchema objectSchema = new ObjectSchema();
        objectSchema.setTitle("Name");
        objectSchema.setDescription("Description");

        NumberSchema prop1 = new NumberSchema();
        prop1.setId("Prop1");
        prop1.setRequired(true);
        objectSchema.putProperty("Prop1", prop1 );

        JsonSchema prop2 = new ObjectSchema();
        prop2.setId("Prop2");
        objectSchema.putProperty("Prop2", prop2 );
        try{            
        System.out.println(mapper.writerWithDefaultPrettyPrinter()
        .writeValueAsString(objectSchema));
        } catch (JsonProcessingException e) {
        e.printStackTrace();
        }
output:-

{
   "type":"object",
   "description":"Description",
   "title":"Name",
   "properties":{
      "Prop1":{
         "type":"number",
         "id":"Prop1",
         "required":true
      },
      "Prop2":{
         "type":"object",
         "id":"Prop2",
         "required":true
      }
   }
}

But i require output in this manner :-

{
   "type":"object",
   "description":"Description",
   "title":"Name",
   "properties":{
      "Prop1":{
         "type":"number",
         "id":"Prop1"
      },
      "Prop2":{
         "type":"object",
         "id":"Prop2"
      }
   },
   "required":[
      "Prop1","Prop2"
   ]
}

Please help me. Thanks in advance.

Henry H. Andrews

unread,
Aug 30, 2017, 6:14:59 PM8/30/17
to JSON Schema
Hi Anup,
  This mailing list is for discussions of the standard, how to implement it, and how to write schemas.

What you are talking about here is a specific implementation which is out of date.  You will need to contact the author/maintainer of that implementation to get help with it, or to request support for more recent drafts.

Or, you can see if there is another implementation that will meet your needs with current draft support:  http://json-schema.org/implementations.html

thanks, and good luck!
-henry
Reply all
Reply to author
Forward
0 new messages