Home > Software design >  How to create a JsonObject of complex structure like given below in Java?
How to create a JsonObject of complex structure like given below in Java?

Time:12-09

{
    "A": {
        "B": {
            "C": {
                "D": "123"
            },
            "E": {
                "F": {
                    "G": "aaabbb"
                },
                "H": "QWE",
                "I": {
                    "J": "003",
                    "K": "01"
                }
            },
            "L": {
                "M": {
                    "N": "1603",
                    "O": "QWE"
                },
                "P": "N",
                "Q": "N"
            },
            "R": {
                "S": "10000",
                "T": "QWE"
            },
            "U": {
                "V": "0",
                "W": "13"
            },
            "X": {
                "Y": "123456"
            }
        },
        "ABC": {
            "QQ": "5",
            "WW": "O"
        }
    }
}

I want to create a similar Json String without having to create all these classes A, B, C, etc. I could create it by creating classes for each of these tags A, B, C, etc using ObjectMapper of Jackson Library. Is there a simpler way to achieve this?

CodePudding user response:

As already stated in my comment, there are a couple of ways.

Nested Maps

ObjectMapper om = new ObjectMapper();
    
Map<String, Map<String, Map<String, Map<String, Object>>>> map = new HashMap<>();
    
map.computeIfAbsent("A", k -> new HashMap<>())
   .computeIfAbsent("B", k -> new HashMap<>())
   .computeIfAbsent("C", k -> new HashMap<>())
   .put("D", "123");
        
((Map<String, String>)map.computeIfAbsent("A", k -> new HashMap<>())
     .computeIfAbsent("B", k -> new HashMap<>())
     .computeIfAbsent("E", k -> new HashMap<>())
     .computeIfAbsent("F", k -> new HashMap<String, String>()))
     .put("G", "aaabbb");       
    
String json = om.writeValueAsString(map);

Methods like computeIfAbsent() help a lot in in having to check for the presence of intermediate maps all the time but as you can see, if the structure is not uniform this can get awkward to handle.

Of course you can build the maps in other ways, this is just a simple demonstration.

Using ObjecNode (and ArrayNode if required)

ObjectMapper om = new ObjectMapper();

ObjectNode root = om.createObjectNode();
ObjectNode nodeA = om.createObjectNode();
root.set("A", nodeA);
    
ObjectNode nodeB = om.createObjectNode();
nodeA.set("B", nodeB);
    
ObjectNode nodeC = om.createObjectNode();
nodeB.set("C", nodeC);
nodeC.put("D", "123");
    
ObjectNode nodeE = om.createObjectNode();
nodeB.set("E", nodeE);
    
ObjectNode nodeF = om.createObjectNode();
nodeE.set("F", nodeF);
nodeF.put("G", "aaabbb");

String json = om.writeValueAsString(root);

Again there are multiple ways to construct this node hierarchy.

Both examples produce the following (unformatted) json which reflects the first portion of your structure:

{"A":{"B":{"C":{"D":"123"},"E":{"F":{"G":"aaabbb"}}}}}
  • Related