Aishwarya
Aishwarya

Reputation: 1087

Unable to put null values in JSON object

I am trying to pass parameter to api using JSON.

class Sample
{ ...
   String token;
...

void method()
{ ...
    JSONObject params = new JSONObject();
    params.put(KEY_TOKEN,token);
    params.put(KEY_DATE,date);

    Log.e("params ",params+"");


      ...  }    

I get the value of params as {"date":"2017-06-19"} but token is seen nowhere. I have not initialized token and its value is null as its an instance variable. So is it something that uninitialized value are not included?

Upvotes: 53

Views: 101389

Answers (7)

Nikhil Kulkarni
Nikhil Kulkarni

Reputation: 674

To include null values in the output JSON string when serializing a JSON object using Google's Gson library in Java, you will need to call the serializeNulls() method when building the Gson object. Here's an example:

JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("name", "John");
        jsonObject.addProperty("age", null);

        Gson gson = new GsonBuilder()
                .serializeNulls()
                .create();
        String jsonString = gson.toJson(jsonObject);

        System.out.println(jsonString);

Upvotes: 0

Stephen C
Stephen C

Reputation: 718758

According to RFC 4627, JSON treats the null symbol as a valid value.

The catch is that JSON null is the representation for the Javascript null value. By contrast, the Java version of null is (according to the experts) more closely aligned with Javascript's undefined value.

The original author of the org.json library decided that JSONObject should treat JSON null in a way that is consistent with the Javascript semantics. Hence it is represented (in Java) as JSONObject.NULL ... which is not equal to null.

Upvotes: 15

ota
ota

Reputation: 41

I solved this by creating a class that extends JSONObject and overriding the method put to use JSONObject.NULL when the passed object is null

public class ObjectJSON extends JSONObject {
    public JSONObject put(String key, Object value) throws JSONException {
        return super.put(key, value == null ? JSONObject.NULL : value);
    }
}

Upvotes: 4

Leonardo Lopes
Leonardo Lopes

Reputation: 43

I fixed this "feature" and created this project https://github.com/leonardofel/JSON-java-put-null-fix

making the following code bellow possible:

JSONObject j = new JSONObject().put("myPreciousNull", null);
System.out.println(j.toString());

prints:

{"myPreciousNull":null}

Upvotes: 1

Lars
Lars

Reputation: 636

I solved this problem by creating the null JSON value using a string representation to stuff the null value into the object. Clearly a hack, but it works.

JSONObject obj = new JSONObject("{\"keyword\": null}");
System.out.println(obj); // {"keyword": null}

Upvotes: 4

T.J. Crowder
T.J. Crowder

Reputation: 1074148

Right there in the documentation, in the first paragraph:

Values may not be null, NaNs, infinities, or of any type not listed here.

So yes, it is "...something that null values are not included..." (edit: that was a quote from your original question; your updated question changes it to "uninitialized values" but the default value of an object reference is null, so...)

It's a "feature" of that class, though; JSON itself understands null just fine. Further down in the documentation it says you use a "sentinal value," NULL, to represent null. Which seems...odd. There's a note about it:

Warning: this class represents null in two incompatible ways: the standard Java null reference, and the sentinel value NULL. In particular, calling put(name, null) removes the named entry from the object but put(name, JSONObject.NULL) stores an entry whose value is JSONObject.NULL.

So:

params.put(KEY_TOKEN, token == null ? JSONObject.NULL : token);

Upvotes: 104

Priya Jain
Priya Jain

Reputation: 830

Json itself accpets null but JSONOBJECT class does not.Hence, you are not able to do that. Also Try using jackson/gson for json and object mapping instead. If you need help with it, let me know.

Upvotes: 1

Related Questions