Asif Billa
Asif Billa

Reputation: 1071

How to exclude a Java Object Property as part of JSON String?

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?

Upvotes: 3

Views: 9055

Answers (2)

takeoffjava
takeoffjava

Reputation: 529

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;
    }

Upvotes: 3

Joaquin Iurchuk
Joaquin Iurchuk

Reputation: 5637

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;

Upvotes: 4

Related Questions