-1
import org.json.JSONObject;

String someStringJsonData = "{\"someKey\": " + "null" + "}"; 
JSONObject someJsonObjectData = new JSONObject(someStringJsonData); 
Object someKey = someJsonObjectData.get("someKey");  
if (null == someKey) {                 
        System.out.println("someKey is null");                             
}

I have the above simple snippet of code. I would expect to see "someKey is null" printed, however, my code never goes in the if loop. I tried printing value of someKey and it shows to be null. Not sure what I am missing.

I tried different values of the jsonString from this post but to no avail.

shailesh
  • 1
  • 2
  • Which `JSONObject` are you actually using? gson does not have a class called `JSONObject`, only [`JsonObject`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/JsonObject.html). – Turing85 Jan 06 '23 at 23:39
  • I am using-> import org.json.JSONObject; – shailesh Jan 06 '23 at 23:40
  • The [documentation of `JSONObject::get`](https://stleary.github.io/JSON-java/org/json/JSONObject.html#get-java.lang.String-) states that this method does throw an exception when the key is not found. – Turing85 Jan 06 '23 at 23:42
  • The link to the post does not link to a post. – Turing85 Jan 06 '23 at 23:43
  • Here the key is there but the value for it is null. It does throw a JSONException if I was doing `someJsonObjectData.get("someOtherKey"); ` – shailesh Jan 06 '23 at 23:44
  • Seems like a natural troubleshooting step would be to check what non-null value is in fact retrieved. Probably it is `JSONObject.NULL` as opposed to a Java `null`. – John Bollinger Jan 06 '23 at 23:45
  • What does `System.out.println(someKey.getClass());` print? – Bohemian Jan 06 '23 at 23:47
  • Please [edit] the post and add a [MRE]. Looking at the [relevant source code of `JSONObject`](https://github.com/stleary/JSON-java/blob/master/src/main/java/org/json/JSONObject.java#L564), it should throw an exception, even if the key is present, but the value associated is `null`. – Turing85 Jan 06 '23 at 23:47

2 Answers2

1

Per its docs, the internal form of the value of a JSONObject can contain values of these types: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object. Therefore, I would not expect its get() method to return null for any key. In your particular case, I would expect someJsonObjectData.get("someKey") to return JSONObject.NULL, which will compare unequal to null.

JSONObject.NULL is a specific object, so it should be safe to perform == comparisons with it:

import org.json.JSONObject;

// ...

    void doSomething() {
        String someStringJsonData = "{\"someKey\": null}"; 
        JSONObject someJsonObjectData = new JSONObject(someStringJsonData); 
        Object someValue = someJsonObjectData.get("someKey");  

        if (someValue == JSONObject.NULL) {                 
            System.out.println("someKey's value is null");                             
        }
    }
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

I needed to do the check for

JSONObject.NULL and not null https://developer.android.com/reference/org/json/JSONObject.html#NULL.

Printing the class of someKey helped. Thank you everyone for your time!!

shailesh
  • 1
  • 2