The JSON object I\'m receiving looks like this:
[{\"foo1\":\"bar1\", \"foo2\":\"bar2\", \"problemkey\": \"problemvalue\"}]
What I\'m trying
if (jsonObj != null && jsonObj.length > 0)
To check if a nested JSON object is empty within a JSONObject:
if (!jsonObject.isNull("key") && jsonObject.getJSONObject("key").length() > 0)
Use the following code:
if(json.isNull()!= null){ //returns true only if json is not null
}
Try:
if (record.has("problemkey") && !record.isNull("problemkey")) {
// Do something with object.
}
@Test
public void emptyJsonParseTest() {
JsonNode emptyJsonNode = new ObjectMapper().createObjectNode();
Assert.assertTrue(emptyJsonNode.asText().isEmpty());
}
Try /*string with {}*/ string.trim().equalsIgnoreCase("{}"))
, maybe there is some extra spaces or something
For this case, I do something like this:
var obj = {};
if(Object.keys(obj).length == 0){
console.log("The obj is null")
}