Sometimes, you don't want everything.

That's especially true when you're using Jackson to serialize a Java object in your Spring Boot application.

You might have an object with empty strings, empty lists, and empty arrays.

If you serialize all that stuff, you'll just create a bunch of clutter in the JSON object.

So why not ignore it?

Fortunately, Jackson makes it easy to ignore the empties.

With @JsonInclude

There's a nifty little annotation called @JsonInclude that you can use for that very purpose. 

Let's take a look at how to use it by first creating an Employee class with some empty defaults:

public class Employee {

    private String id;    
    private String lastName;
    private String firstName;    
    private List<String> groups = new ArrayList<>();
    private String shirtSize = "";
    
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    
    public List<String> getGroups() {
        return groups;
    }

    public void setGroups(List<String> groups) {
        this.groups = groups;
    }

    public String getShirtSize() {
        return shirtSize;
    }

    public void setShirtSize(String shirtSize) {
        this.shirtSize = shirtSize;
    }

    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }
}

So the groups property is an empty List object and the shirtSize property is an empty string.

Note: neither one of those is null. They're just empty.

Now let's serialize an instance of that object using this code:

ObjectMapper mapper = new ObjectMapper();            

Employee employee = new Employee();
employee.setId("A17");
employee.setFirstName("Frank");
employee.setLastName("Smith");

System.err.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee));

And we'll end up with this output:

{
  "id" : "A17",
  "lastName" : "Smith",
  "firstName" : "Frank",
  "groups" : [ ],
  "shirtSize" : ""
}

As you can see, the "groups" and "shirtSize" properties got serialized with empty results.

If that's what you want to avoid, read on.

Avoiding That

Now go back to that Employee class and update it with the @JsonInclude annotation as follows:

@JsonInclude(Include.NON_EMPTY)
public class Employee {
...

That code uses the Include.NON_EMPTY option with @JsonInclude.

What does that do? For starters, it excludes empty strings, empty arrays, and empty List objects.

But it also excludes null values.

Additionally, it will exclude default values for primitives (like 0 for int and false for boolean). 

It will even exclude Timestamp 0 for date/time types.

Also, it excludes empty Optional objects.

So Include.NON_EMPTY excludes a lot more than just empty arrays and strings. That's something you need to keep in mind when you're using it.

Anyhoo, if you run that serialization code again after adding the annotation, you'll see this result:

{
  "id" : "A17",
  "lastName" : "Smith",
  "firstName" : "Frank"
}

And that's exactly what you're looking for. No more empties.

Wrapping It Up

So now you know how to exclude empty strings, arrays, and List objects from your Jackson serialization processes.

All you need to do at this point is apply what you've learned here to your own Spring Boot development efforts. Why not get started today?

Have fun!

Photo by Darius Krause from Pexels