Table of Contents

How to exclude properties from the JSON output with Jackson?

About

This article lists the techniques that may be used with the Jackson libary to filter out some properties of an object when transforming it as a JSON.

Filter in Jackson means that the data are processed not deleted

Techniques

There are 5 techniques that you can apply to filter out properties:

JsonFilter

JsonFilter is an annotation property that permits applying a filter when processing.

By default, the standard given filters permit to exclude properties but you can also just apply a transformation if you create your own filter.

Example:

Example on a minimal user:

public class User   {

  private String id;
  private String name;

  @JsonFilter("private")
  @JsonProperty("id")
  public String getId() {
    return id;
  }

  @JsonProperty("name")
  public String getName() {
    return name;
  }


}
String jsonString = JsonMapper.builder()
      .build()
      .setFilterProvider(
        // Add a filter named private that process all properties (filter) except the property id
        new SimpleFilterProvider()
          .addFilter("private", SimpleBeanPropertyFilter.filterOutAllExcept("id"))
      )
      .writeValueAsString(userPojo);

JsonView

JsonView 1) is a property/field annotation that adds a property to a view. When processing, you can then select the view that you want to render.

Example:

public class Views {

  public static class Public {
  }

}
public class User   {

  private String id;
  private String name;

  @JsonProperty("id")
  public String getId() {
    return id;
  }

  @JsonView(Views.Public.class)
  @JsonProperty("name")
  public String getName() {
    return name;
  }


}
String jsonString = JsonMapper.builder()
      // By default, all properties not explicitly annotated with a JsonView 
      // is part of the view
      // The below code disables this behavior
      .disable(MapperFeature.DEFAULT_VIEW_INCLUSION)
      .build()
      .writerWithView(Views.Public.class)
      .writeValueAsString(userPojo);

Ignore

This section lists annotations for ignoring properties 3).

JsonIgnore

With the @JsonIgnore annotation 4), the property will be completely ignored

Example: ignore the id field completely.

public class User   {

  private String id;
  private String name;
  
  @JsonIgnore
  public String getId() {
    return id;
  }

  @JsonProperty("name")
  public String getName() {
    return name;
  }


}

JsonIgnoreProperties

@JsonIgnoreProperties is a per-class annotation to list properties to ignore, or to indicate that any unknown properties are to be ignored.

@JsonIgnoreProperties({"prop1", "prop2"}) 
@JsonIgnoreProperties(ignoreUnknown=true) 

JsonIgnoreType

@JsonIgnoreType, a per-class annotation to indicate that all properties of annotated type are to be ignored

Visibility

Visibility (ie Public / Private) lets Jackson see or not the property.

Private/Public

You can manage the visibility with accessors:

JsonAutoDetect

You can configure them with the Visibility enum 5)

ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)

Mixin

A Mixin 6) permits overwriting the JSON annotations on the fly.

Mix-ins are useful for applying Jackson configuration to classes without modifying the source code of the target class.

They can be thought of as a configuration layer that sits above a class that Jackson will look at for instructions during de/serialization.

abstract class UserMixin {


  @JsonIgnore
  abstract Long getId();

}
String jsonString = JsonMapper.builder()
      .build()
      .addMixIn(User.class, UserMixin.class)
      .writeValueAsString(userPojo);