Skip to content
Advertisement

Jackson 2.11.4: Set Default Value For Missing JSON field

I have the below JSON

JavaScript

below is the POJO and I need to have both the constructors because if there is no choiceType in json I need to default it to Yes, if choiceType in json exists with null value then it should not default to Yes

JavaScript

And my goal is to have a list of choices when the above Json is deserialized and I have below test case

JavaScript

When I try to deserialize the below json. I have tried many solutions but still no luck, can someone please help me witht this?

JavaScript

Advertisement

Answer

You could just get rid of the args constructor and default the value to “Yes”. If value is explicitly set to null or to a value it will be assigned. In case of missing value it will be defaulted.

JavaScript

Reference – Jackson: What happens if a property is missing?

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement