56

I have a User class that I want to map to JSON using Jackson.

public class User {
    private String name;
    private int age;
    private int securityCode;

    // getters and setters
}

I map this to a JSON string using -

User user = getUserFromDatabase();

ObjectMapper mapper = new ObjectMapper();   
String json =  mapper.writeValueAsString(user);

I don't want to map the securityCode variable. Is there any way of configuring the mapper so that it ignores this field?

I know I can write custom data mappers or use the Streaming API but I would like to know if it possible to do it through configuration?

sigpwned
  • 6,957
  • 5
  • 28
  • 48
sonicboom
  • 4,928
  • 10
  • 42
  • 60
  • See also https://stackoverflow.com/questions/34965201/customize-jackson-objectmapper-to-read-custom-annotation-and-mask-fields-annotat – Vadzim May 16 '19 at 15:35

8 Answers8

82

You have two options:

  1. Jackson works on setters-getters of fields. So, you can just remove getter of field which you want to omit in JSON. ( If you don't need getter at other place.)

  2. Or, you can use the @JsonIgnore annotation of Jackson on getter method of that field and you see there in no such key-value pair in resulted JSON.

    @JsonIgnore
    public int getSecurityCode(){
       return securityCode;
    }
    
Kenny Linsky
  • 1,726
  • 3
  • 17
  • 41
Ravi Khakhkhar
  • 1,924
  • 1
  • 18
  • 25
  • 10
    Can I hide based on some condition? Say if name is admin then I need to show else hide? – foobar Aug 01 '17 at 12:30
  • 2
    @foobar did you find any work around for ur case? I have same cases as yours – Akza Jul 05 '19 at 09:28
  • 1
    I'm also looking for hiding Json serialization of specific properties based on some condition. Did anyone find an answer ? – anjanb Dec 16 '19 at 10:34
  • 1
    I feel if you use Lombok you can use as below, @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) – Rajitha Bhanuka May 27 '21 at 08:59
27

Adding this here because somebody else may search this again in future, like me. This Answer is an extension to the Accepted Answer

You have two options:

1. Jackson works on setters-getters of fields. So, you can just remove getter of field which you want to omit in JSON. ( If you don't need getter at other place.)

2. Or, you can use the `@JsonIgnore` [annotation of Jackson][1] on getter method of that field and you see there in no such key-value pair in resulted JSON. 

        @JsonIgnore
        public int getSecurityCode(){
           return securityCode;
        }

Actually, newer version of Jackson added READ_ONLY and WRITE_ONLY annotation arguments for JsonProperty. So you could also do something like this.

@JsonProperty(access = Access.WRITE_ONLY)
private String securityCode;

instead of

@JsonIgnore
public int getSecurityCode(){
  return securityCode;
}
so-random-dude
  • 15,277
  • 10
  • 68
  • 113
  • This a good way of doing it! even if the @JsonIgnore may work but I find this useful and very straight to the point. – Ishimwe Aubain Consolateur Nov 29 '18 at 09:25
  • Very, very good answer. But, just for the clarity of options here: `@JsonProperty(access = Access.WRITE_ONLY) private String securityCode;` can then as well be `@JsonProperty(access = Access.WRITE_ONLY) public String securityCode;`. – Der Zinger Nov 28 '22 at 21:44
20

you also can gather all properties on an annotation class

@JsonIgnoreProperties( { "applications" })
public MyClass ...

String applications;
Benjamin Fuentes
  • 674
  • 9
  • 22
  • This is great option for classes with @Data lombok annotation, because you don't have much control (only access levels) over the generated getters and setters – user1927829 Jul 27 '21 at 13:53
7

If you don't want to put annotations on your Pojos you can also use Genson.

Here is how you can exclude a field with it without any annotations (you can also use annotations if you want, but you have the choice).

Genson genson = new Genson.Builder().exclude("securityCode", User.class).create();
// and then
String json = genson.serialize(user);
eugen
  • 5,856
  • 2
  • 29
  • 26
  • 5
    Jackson also offers multiple alternatives to annotating your pojo classes directly; see http://wiki.fasterxml.com/JacksonMixInAnnotations – pimlottc Apr 06 '15 at 19:49
2

Field Level:

public class User {
    private String name;
    private int age;
    @JsonIgnore
    private int securityCode;

    // getters and setters
}

Class Level:

@JsonIgnoreProperties(value = { "securityCode" })
public class User {
    private String name;
    private int age;
    private int securityCode;
}
CamelTM
  • 1,225
  • 12
  • 17
1

if you are using GSON you have to mark the field/member declarations as @Expose and use the GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()

Don't forget to mark your sub classes with @Expose otherwise the fields won't show.

tak
  • 21
  • 7
1

I suggest you use this.

   @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
   private int securityCode;

This allows you to set the value of securityCode(especially if you use lombok @Setter) and also prevent the field from showing up in the GET request.

Arnoldkk
  • 489
  • 7
  • 11
0

I had a similar case where I needed some property to be deserialized (JSON to Object) but not serialized (Object to JSON)

First i went for @JsonIgnore - it did prevent serialization of unwanted property, but failed to de-serialize it too. Trying value attribute didn't help either as it requires some condition.

Finally, working @JsonProperty with access attribute worked like a charm.

Milo
  • 3,365
  • 9
  • 30
  • 44
Puneet
  • 11
  • 3