Jackson ObjectMapper JSON check for null doesnt work

Viewed 30

Currently i'm trying to check if the key value in my json file is null then i will return false, however it doesnt seem to work.

This is my null-checking code

jsonGet = ObjectMapper().readTree(//json file);
if(jsonGet.get("key1") == null){
return false; //end program
}
//do something else

Tho i set that key value in my json file like so, it also said "key1 => null" in my watch

 "key1": null,

It somehow doesnt jump into the return false, dont really know what im doing wrong here

2 Answers

Well there seem to be some kind of issue regarding getting the null value, so for now i'll just use check instance since the return type is NullNode for some reason

if(jsonGet.get("key1") instanceof NullNode){
return false; //end program
}

I can't find here class of jsonGet variable. Maybe it's JsonNode class. Then JsonNode.get(String fieldName) method returns JsonNode object (javadoc for JsonNode). Then I think you should use textValue() method or smth similar.

But here can be another problem. Maybe you will get String object with value "null". Then you will need to check for this condition (I'm not sure about it).

Related