i have to write a Java class for the json string. below is my json string, in which it has 'properties' attribute , any key-value pair can come and value is again some json object..like for 'type' string , it can have different attributes, for 'type' number it can have different attributes ..etc.

    "type": "object",
    "required": [
    "properties": {
        "a": {
            "type": "number",
            "description": "this is column a"
        "b": {
            "type": "date",
            "description": "this is column b"
        "fullName": {
            "type": "string",
            "description": "",
            "minLength": 1,
            "maxLength": 10
    "title": "this is table",
    "name": "test_table"        

Below is the code which I have tried and it worked

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.db.gtb.cdo.model.datatype.DataTypeModel;

import lombok.Data;

public class TableModel {

    private String type;
    private List<String> required = new ArrayList<String>();
    private Map<String, DataTypeModel> properties = new LinkedHashMap<>();
    private String title;


   public abstract class DataTypeModel {

    DataType type;
    String description;

    public DataTypeModel(DataType type, String description) {
        this.type = type;
        this.description = description;

    public DataType getType() {
        return type;

    public String getDescription() {
        return description;


import lombok.Getter;
import lombok.Setter;

public class StringDataTypeModel extends DataTypeModel {
    @Setter @Getter Integer minLength;
    @Setter @Getter Integer maxLength;

    public StringDataTypeModel(DataType type, String description, Integer minLength, Integer maxLength) {
        super(type, description);
        this.minLength = minLength;
        this.maxLength = maxLength;

 public class NumberDataTypeModel extends DataTypeModel {

    public NumberDataTypeModel(DataType type, String description) {
    super(type, description);


public class DateDataTypeModel extends DataTypeModel {

    public DateDataTypeModel(DataType type, String description) {
        super(type, description);


import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class Test {

    public static void main(String args[]) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);

        TableModel table = new TableModel();

        Map<String, DataTypeModel> properties = new HashMap<>();

        properties.put("code", new StringDataTypeModel(DataType.string, "this is string", 1, 15,"/s"));
        properties.put("startDate", new DateDataTypeModel(DataType.date, "this is date", "pattern"));

        table.setTitle("this is branch table for bank");

        mapper.writer().withRootName("branch").writeValue(new File("Table.json"), table);

        //Object to JSON in String
        String jsonInString = mapper.writer().withRootName("branch").writeValueAsString(table);

The above class generates the the expected json structure

1 Answers

LeoN On Best Solutions