3

I have a Java POJO. Few properties are there along with a list<>. While converting this object to JSON String, I want to exclude the list property, So what annotation to use for that?

public class StudentResultSummary {
    private String totMarks;
    private String avgMarks;
    private List<StudentResult> resultList = new ArrayList<StudentResult>();
}

Convert to JSON:

StudentResultSummary resultSummary = new StudentResultSummary();
Json json = new Json();
policySummary = json.encode(resultSummary);

How can I make sure the field resultList is not included as part of the JSON response?

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
Asif Billa
  • 821
  • 2
  • 15
  • 28

2 Answers2

4

From Chris Seline's answer:

Any fields you don't want serialized in general you should use the "transient" modifier, and this also applies to json serializers (at least it does to a few that I have used, including gson).

If you don't want name to show up in the serialized json give it a transient keyword, eg:

private transient String name;
Community
  • 1
  • 1
Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64
3

If You don't want to that column in response json you can use @JsonIgnore and if you don't want to that field in table you should use @Transient

 @Transient
    private String password_key_type; 

    @JsonIgnore
    public int getUser_id()
    {
    return user_id;
    }
takeoffjava
  • 504
  • 4
  • 15