Checkout for Promo Codes

How to exclude json fields in Gson APIs?

This page shows how to exclude certain json fields when using Gson APIs. It is a 2 step process. Firstly annotate your POJO member variables with @Expose annotation, remember that don't annotate the fields which you don't want to be part of json output. As a second step, you must create Gson by using new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(). Now the json output will include all members which has annotated with @Expose annotation.

Note: Refer How to convert java object to json string using Gson APIs? page for dependent libraries.

Here is the sample POJO which has members annotated with @Expose annotation. Notice that "salary" field is not annotated with @Expose annotation which is not part of the output.

package com.java2novice.json.gson;


public class Employee {

	private int empId;
	private String name;
	private String designation;
	private String department;
	private int salary;
	public int getEmpId() {
		return empId;
	public void setEmpId(int empId) {
		this.empId = empId;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getDesignation() {
		return designation;
	public void setDesignation(String designation) {
		this.designation = designation;
	public String getDepartment() {
		return department;
	public void setDepartment(String department) {
		this.department = department;
	public int getSalary() {
		return salary;
	public void setSalary(int salary) {
		this.salary = salary;

Note that the Gson obect creation is using excludeFieldsWithoutExposeAnnotation() method call.

package com.java2novice.json.gson;


public class ObjectToJsonEx {

	public static void main(String a[]){
		Employee emp = new Employee();
		emp.setName("Nataraj G");
		Gson gsonObj = new GsonBuilder()
		// converts object to json string
		String jsonStr = gsonObj.toJson(emp);

  "emp_id": 1016,
  "emp_name": "Nataraj G",
  "designation": "Manager",
  "department": "Accounts"
<< Previous Program 

Google gson examples

  1. How to convert java object to json string using Gson APIs?
  2. How to convert json string to java object using Gson APIs?
  3. How to convert map to json string using Gson APIs?
  4. How to enable pretty printing in Google gson APIs?
  5. How to rename JSON properties (@SerializedName) using Gson APIs?
  6. How to exclude json fields in Gson APIs?
Knowledge Centre
Difference between while and do-while
The while statement verifies the condition before entering into the loop to see whether the next loop iteration should occur or not. The do-while statement executes the first iteration without checking the condition, it verifies the condition after finishing each iteration. The do-while statement will always execute the body of a loop at least once.
Famous Quotations
The pessimist complains about the wind; the optimist expects it to change; the realist adjusts the sails.
-- William Arthur Ward

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.