'How to remove the empty elements from the json using object mapper java

So I have a object mapper (jackson version is 2.9.5) with following values:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        

but when the final json is created I still see the empty elements like below:

{"address":[],"customerKycDetails":[],"security":[]}

So can I know how exactly to configure the objectMapper to remove all the empty object like this: "security":[]?

I was thinking JsonInclude.Include.NON_EMPTY should work but it is not working.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source