Home > Enterprise >  Spring Boot nested dynamic json request mapping to pojo
Spring Boot nested dynamic json request mapping to pojo

Time:11-01

I am building a REST API that accepts input in the following JSON format from users. The specifics are nested JSON objects whose key value is dynamic and its associated data type is also dynamic. Like in below color is a List of String and ram is a list of Integer and also value could be of type string, boolean, and integer. How to build Request POJO class to transform request JSON into Request Object considering specifics is dynamic in key, value, and its data type. In the below JSON, specifics can have 0..n key-value, and its value data type could be List of String, List of Integer, String only, boolean only, or Just Integer value. How to map my request to a POJO in the spring boot backend.

 {
        name : {
            subtitle : "iPhone xr",
            title: "The new iphone"
        },
    
        specifics: {
          
             "color" : ["red","blue","green"],
    
             "ram" :  [ 32 , 64 ],
    
             "length": 7,
    
             "model" : 10 ,
    
             "hasLight" : true 
          
        },
    
        description: {
          short: " This is short description",
          long:  " This is long description ",
          language: "en"
        }
    
    }

Class

 class Item{
       private Title title;
         private Specifics specifics;  //How to make this dynamic and key-value with distinct data type
       private Description description;
   }

  public class Title {
    private String subtitle;
    private String title;
  }


   public class Description {
    private String shortDes;
    private String longDes;
    private String language;
  }

CodePudding user response:

Instead of using a POJO class to transform Request JSON into a Request Object, you could use a Map. Then you have to analyse the Map and transform it as you need.

CodePudding user response:

The only possible way to deal with this that I can think of is making specifics property a Map as follows:

class Item {
    private Title title;
    private Map<String, Object> specifics;
    private Description description;
}

Now the ugly part is when you need (if you actually need) to process the data. Consider the following example:

public static void main(String[] args) {
    Map<String, Object> specifics = new HashMap<>();
    specifics.put("color", List.of("red","blue","green"));
    specifics.put("ram", List.of(32 , 64));
    specifics.put("length", 7);
    specifics.put("model", 10);
    specifics.put("hasLight", true);

    Item item = new Item(specifics);

    for (Object object : item.getSpecifics().values()) {
        if (object instanceof List) {
            System.out.println("::::: List :::::");
            for (Object listObject : (List) object) {
                if (String.class.isAssignableFrom(listObject.getClass())) {
                    System.out.println("String: "   object);
                }
                if (Number.class.isAssignableFrom(listObject.getClass())) {
                    System.out.println("Number: "   object);
                }
            }
        }
        if (object instanceof Number) {
            System.out.println("Number: "   object);
        }
        if (object instanceof Boolean) {
            System.out.println("Boolean: "   object);
        }
    }
}

public static class Item {
    private Map<String, Object> specifics;

    public Item(Map<String, Object> specifics) {
        this.specifics = specifics;
    }

    public Map<String, Object> getSpecifics() {
        return specifics;
    }
}

As you can see the logic to handle every single possibility is ugly and can only become uglier with additional possibilities. Unfortunately, there is no around it.

  • Related