I am trying to deserialize nested JSON into a single DTO in a Spring Boot application. The JSON:
{"productId": "xyz123",
"description": "some_description",
"value": "123",
"boughtOnDate": "2020-05-20 14:22:58.000662",
"details": {
"material": "some_material",
/// another 20 entries
}
}
The DTO:
@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
public class Item {
@Id
private String productId;
private String description;
private Integer value;
private Timestamp boughtOnDate;
private String material;
// another 20 fields that are in the nested part of the json
// getters & setters
The current solution I have is to unpack nested json in the Item class like so:
@JsonProperty("details")
private void unpackNestedJson(Map<String, Object> details) {
this.material = (String) details.get("material");
// and another 20 lines for unpackacking the rest from the nested part
While unpackNestedJson works fine and does its job, it feels cumbersome as there is a lot of data to unpack from the nested part of the JSON (all entries below and above 'details' from JSON should go as fields into Item entity class only). My question would be - is there a more simple/elegant way of unpacking the nested JSON into a single DTO? Any help appreciated.