How to test if JSON object is empty in Java-ThrowExceptions

Exception or error:

The JSON object I’m receiving looks like this:

[{"foo1":"bar1", "foo2":"bar2", "problemkey": "problemvalue"}]

What I’m trying to test for is the existence of problemvalue. If problemvalue returns a JSON Object, I’m happy. If it doesn’t, it will return as {}. How do I test for this condition? I’ve tried several things to no avail.

This is what I’ve tried thus far:

//      if (obj.get("dps") == null) {  //didn't work
//      if (obj.get("dps").equals("{}")) {  //didn't work
if (obj.isNull("dps")) {  //didn't work
    System.out.println("No dps key");
}

I expected one of these lines to print “No dps key” because {"dps":{}}, but for whatever reason, it’s not. I’m using org.json. The jar file is org.json-20120521.jar.

How to solve:
obj.length() == 0

is what I would do.

Answer:

If you’re okay with a hack –

obj.toString().equals("{}");

Serializing the object is expensive and moreso for large objects, but it’s good to understand that JSON is transparent as a string, and therefore looking at the string representation is something you can always do to solve a problem.

Answer:

If empty array:

.size() == 0

if empty object:

.length() == 0

Answer:

A JSON notation {} represents an empty object, meaning an object without members. This is not the same as null. Neither it is string as you are trying to compare it with string “{}”.
I don’t know which json library are you using, but try to look for method something like:

isEmptyObject() 

Answer:

Try:

if (record.has("problemkey") && !record.isNull("problemkey")) {
    // Do something with object.
}

Answer:

I would do the following to check for an empty object

obj.similar(new JSONObject())

Answer:

I have added isEmpty() methods on JSONObject and JSONArray()

 //on JSONObject 
 public Boolean isEmpty(){         
     return !this.keys().hasNext();
 }

//on JSONArray
public Boolean isEmpty(){
    return this.length()==0;        
}

you can get it here https://github.com/kommradHomer/JSON-java

Answer:

Object getResult = obj.get("dps"); 
if (getResult != null && getResult instanceof java.util.Map && (java.util.Map)getResult.isEmpty()) {
    handleEmptyDps(); 
} 
else {
    handleResult(getResult); 
}

Answer:

If JSON returned with following structure when records is an ArrayNode:

{}client 
  records[]

and you want to check if records node has something in it
then you can do it using a method size();

if (recordNodes.get(i).size() != 0) {}

Answer:

@Test
public void emptyJsonParseTest() {
    JsonNode emptyJsonNode = new ObjectMapper().createObjectNode();
    Assert.assertTrue(emptyJsonNode.asText().isEmpty());
}

Answer:

if you want to check for the json empty case, we can directly use below code

String jsonString = {};
JSONObject jsonObject = new JSONObject(jsonString);
if(jsonObject.isEmpty()){
 System.out.println("json is empty");
} else{
 System.out.println("json is not empty");
}

this may help you.

Answer:

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)

Answer:

Try /*string with {}*/ string.trim().equalsIgnoreCase("{}")), maybe there is some extra spaces or something

Answer:

Use the following code:

if(json.isNull()!= null){  //returns true only if json is not null

}

Answer:

For this case, I do something like this:

var obj = {};

if(Object.keys(obj).length == 0){
        console.log("The obj is null")
}

Leave a Reply

Your email address will not be published. Required fields are marked *