Jackson will give you null for other objects, but for String it will give empty String.

But you can use a Custom JsonDeserializer to do this:

class CustomDeserializer extends JsonDeserializer<String> {

    @Override
    public String deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException {
        JsonNode node = jsonParser.readValueAsTree();
        if (node.asText().isBlank()) {
            return null;
        }
        return node.toString();
    }

}

In class you have to use it for location field:

class EventBean {
    public Long eventId;
    public String title;

    @JsonDeserialize(using = CustomDeserializer.class)
    public String location;
}
Answer from Sachin Gupta on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 25359245 › unable-to-convert-null-string-into-json-object
java - Unable to convert null String into JSON object - Stack Overflow
May 23, 2017 - use a json parser to convert it into an correct json encoded string i used org.json.parser to convert ... You have null values. JSONObject can't accept null values. See How do you set a value to null with org.json.JSONObject in java?
🌐
CodingTechRoom
codingtechroom.com › question › deserialize-empty-json-string-null-java
How to Deserialize an Empty JSON String to null for java.lang.String in Java? - CodingTechRoom
ObjectMapper objectMapper = new ObjectMapper(); String jsonString = ""; // Represents the empty JSON string String result = objectMapper.readValue(jsonString, String.class); // result will be null if jsonString is empty. In Java, when working with JSON data, you may encounter scenarios where you want to deserialize an empty JSON string (`""`) to `null` for `java.lang.String`. By default, most JSON libraries will return an empty string rather than `null`. To handle this appropriately, we will utilize the Jackson library, which offers flexibility in how deserialization operates.
🌐
Stack Overflow
stackoverflow.com › questions › 20654810 › jackson-deserializing-null-strings-as-empty-strings
java - Jackson: deserializing null Strings as empty Strings - Stack Overflow
I have the following class, that is mapped by Jackson (simplified version): public class POI { @JsonProperty("name") private String name; } In some cases the server returns "name": null an...
Find elsewhere
🌐
Google Groups
groups.google.com › g › google-gson › c › W3eXzqCnZ6U
Replace null with empty string on serialization
December 2, 2010 - ... Either email addresses are ... work for me, so i ended up doing like this using Gson gson = new GsonBuilder().serializeNulls().create(); String json = gson.toJson(obj5); json = json.replaceall("null","\"\""); may be it will be slow if u have very large string with null ...
🌐
GitHub
github.com › FasterXML › jackson-module-kotlin › issues › 440
Kotlin FeatureRequest: Serialize null string to empty string · Issue #440 · FasterXML/jackson-module-kotlin
May 5, 2021 - @Singleton class JacksonRegisterCustomObjectMapperCustomizer : ObjectMapperCustomizer { override fun customize(mapper: ObjectMapper) { val sp = DefaultSerializerProvider.Impl() sp.setNullValueSerializer(NullSerializer.instance) val stringModule = SimpleModule() stringModule.addSerializer(String::class.javaObjectType, StringSerializer()) stringModule.addSerializer(String::class.javaPrimitiveType, StringSerializer()) stringModule.addSerializer(String::class.java, StringSerializer()) mapper.registerModule(stringModule).setSerializerProvider(sp) } class StringSerializer : JsonSerializer<String?>() { override fun serialize(string: String?, jsonGenerator: JsonGenerator, serializerProvider: SerializerProvider) { when { string.isNullOrBlank() -> jsonGenerator.writeString("") else -> jsonGenerator.writeString(string) } } } }
Author: FasterXML
Top answer
1 of 3
6

Since someone asked this question again a few minutes ago, I did some research and think I found a good solution to this problem (when dealing with Strings).

You have to create a custom JsonDeserializer class as follows:

class NullStringJsonDeserializer extends JsonDeserializer<String> {
    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String result = StringDeserializer.instance.deserialize(p, ctxt);
        return result!=null && result.toLowerCase().equals(null+"") ? null : result;
    }
}

Last but not least, all you have to do is tell your ObjectMapper that it should use your custom json string deserializer. This depends on how and where you use your ObjectMapper but could look like that:

ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(String.class, new NullStringJsonDeserializer());
objectMapper.registerModule(module);
2 of 3
1

If you use a custom Deserializer, you can do this, but I don't think it's available using standard annotations.

Borrowing and modifying some code from http://www.baeldung.com/jackson-deserialization :

public class ItemDeserializer extends JsonDeserializer<Item> {

    @Override
    public Item deserialize(JsonParser jp, DeserializationContext ctxt)
      throws IOException, JsonProcessingException {
        JsonNode node = jp.getCodec().readTree(jp);
        String title = null;
        TextNode titleNode = (TextNode)node.get("title");
        if ( ! titleNode.toString().equals("null")) {
            title = titleNode.toString();
        }

        Date expires = null;
        // similar logic for expires

        return new Item(title, expires);
    }
}
🌐
Salesforce
trailhead.salesforce.com › trailblazer-community › feed › 0D54V00007T4TsVSAV
Converting "Null" string in JSON to BLANK - Trailhead
June 13, 2017 - Skip to main content · Bring your team and maximize your impact at Dreamforce. Register three or more to unlock $999 passes
🌐
Baeldung
baeldung.com › home › json › jackson › include null value in json serialization
Include null Value in JSON Serialization | Baeldung
June 21, 2025 - The toString() method also provides a string representation of the object in a JSON-like format for easy debugging. Please note that including @JsonProperty annotations ensures that all relevant fields are accurately serialized into the JSON ...
Top answer
1 of 8
589

Let's evaluate the parsing of each:

http://jsfiddle.net/brandonscript/Y2dGv/

var json1 = '{}';
var json2 = '{"myCount": null}';
var json3 = '{"myCount": 0}';
var json4 = '{"myString": ""}';
var json5 = '{"myString": "null"}';
var json6 = '{"myArray": []}';

console.log(JSON.parse(json1)); // {}
console.log(JSON.parse(json2)); // {myCount: null}
console.log(JSON.parse(json3)); // {myCount: 0}
console.log(JSON.parse(json4)); // {myString: ""}
console.log(JSON.parse(json5)); // {myString: "null"}
console.log(JSON.parse(json6)); // {myArray: []}

The tl;dr here:

The fragment in the json2 variable is the way the JSON spec indicates null should be represented. But as always, it depends on what you're doing -- sometimes the "right" way to do it doesn't always work for your situation. Use your judgement and make an informed decision.


JSON1 {}

This returns an empty object. There is no data there, and it's only going to tell you that whatever key you're looking for (be it myCount or something else) is of type undefined.


JSON2 {"myCount": null}

In this case, myCount is actually defined, albeit its value is null. This is not the same as both "not undefined and not null", and if you were testing for one condition or the other, this might succeed whereas JSON1 would fail.

This is the definitive way to represent null per the JSON spec.


JSON3 {"myCount": 0}

In this case, myCount is 0. That's not the same as null, and it's not the same as false. If your conditional statement evaluates myCount > 0, then this might be worthwhile to have. Moreover, if you're running calculations based on the value here, 0 could be useful. If you're trying to test for null however, this is actually not going to work at all.


JSON4 {"myString": ""}

In this case, you're getting an empty string. Again, as with JSON2, it's defined, but it's empty. You could test for if (obj.myString == "") but you could not test for null or undefined.


JSON5 {"myString": "null"}

This is probably going to get you in trouble, because you're setting the string value to null; in this case, obj.myString == "null" however it is not == null.


JSON6 {"myArray": []}

This will tell you that your array myArray exists, but it's empty. This is useful if you're trying to perform a count or evaluation on myArray. For instance, say you wanted to evaluate the number of photos a user posted - you could do myArray.length and it would return 0: defined, but no photos posted.

2 of 8
282

null is not zero. It is not a value, per se: it is a value outside the domain of the variable indicating missing or unknown data.

There is only one way to represent null in JSON. Per the specs (RFC 4627 and json.org):

2.1.  Values

A JSON value MUST be an object, array, number, or string, or one of
the following three literal names:

  false null true

🌐
JavaMadeSoEasy
javamadesoeasy.com › 2018 › 09 › how-to-ignore-empty-or-null-values-in.html
JavaMadeSoEasy.com (JMSE): How to ignore empty or null values in JSON java - using Jackson
It will exclude null values. @JsonInclude(Include.NON_EMPTY) It will exclude empty values. We can use Jackson api for for processing JSON in java. Jackson JSON examples · Convert JSON string to java Object - using Jackson · Convert java object to JSON string and pretty print Json in java - using Jackson ·