My function needs to take an input String that can represent any kind of JSON object, clean the empty fields and transform it back to a String.
Given this input JSON, I like the doors
field to be removed:
String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : \"\"}";
I was trying to work with Jackson
and this post and got this:
private static void jacksonConvert(String inputJSON) throws IOException{
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); // tried each of the 3 options. Non of them worked
mapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Object jacksonObj = mapper.readValue(inputJSON, Object.class);
String json = mapper.writeValueAsString(jacksonObj);
System.out.println(json); // {"brand":"Mercedes","doors":""} why "doors" is here?
}
Yet, the doors
field keeps on coming out in the output...
What am I missing here?