Posts

Showing posts from October, 2020

How to ignore empty lists in Jackson?

 Assume you are building a service which pushes the data to the client in JSON format. The data includes some list of objects, sometimes it will available, sometimes it may not. Below is the sample object, which we are using to build the JSON data. I have 4 list properties in this, suppose if one of the list properties is empty, in the JSON format, it will have an empty array. If the client accepts the empty array well and good, if not, we need to exclude it from the payload we are sending. public class ClientData { @JsonIgnore private Long orderId ; @JsonProperty ( "additional_info" ) private AdditionalInfoDTO additionalInfoDTO ; @JsonProperty ( "deed_current" ) private DeedCurrentDTO deedCurrent ; @JsonProperty ( "deeds_prior" ) private List<DeedPriorDTO> deedsPrior ; @JsonProperty ( "judgments_and_liens" ) private List<JudgementAndLienDTO> judgementAndLienDTOS ; @JsonProperty ( "mortgages" ) privat...