Gson – Convert Java object to JSON example shows how to convert a Java object to JSON using the Gson library in Java. This example also shows how to include null fields in the JSON data that are not included by default.
How to convert a Java object to JSON?
The Gson library provides methods to serialize and deserialize Java objects to and from JSON data. For converting a Java object to JSON, we can use the toJson
method of the Gson class.
1 |
public String toJson(Object object) |
The toJson
method serializes the given Java object to a JSON format. In the below given example, I am going to serialize Employee class objects to JSON using this method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package com.javacodeexamples.gsonexamples; import com.google.gson.Gson; public class GsonJavaObjectToJsonExample { public static void main(String[] args) { //create a Gson object Gson gson = new Gson(); Employee emp1 = new Employee("Emp01", "Alex", 65000, "Developer"); /* * Convert a Java object to JSON using the * toJson method */ String strJsonData = gson.toJson(emp1); System.out.println(strJsonData); Employee emp2 = new Employee("Emp02", "John", 92000, null); System.out.println(gson.toJson(emp2)); } } class Employee{ private String Id; private String Name; private double Salary; private String Designation; public Employee(String Id, String Name, double Salary, String Designation){ this.Id = Id; this.Name = Name; this.Salary = Salary; this.Designation = Designation; } public String getId() { return Id; } public void setId(String id) { Id = id; } public String getName() { return Name; } public void setName(String name) { Name = name; } public double getSalary() { return Salary; } public void setSalary(double salary) { Salary = salary; } public String getDesignation() { return Designation; } public void setDesignation(String designation) { Designation = designation; } } |
Output
1 2 |
{"Id":"Emp01","Name":"Alex","Salary":65000.0,"Designation":"Developer"} {"Id":"Emp02","Name":"John","Salary":92000.0} |
As you can see from the output, both of the objects are serialized in the JSON representation. However, by default, the toJson
method skips the null fields. As it happened with our second employee object, the Designation field was not serialized because it was null.
To include null fields in the JSON, we need to create the Gson object using GsonBuilder class and call the searializeNulls
method.
1 |
public GsonBuilder serializeNulls() |
This method allows us to serialize null fields that are omitted by default. Let’s do that and run the program again.
1 2 3 4 5 6 7 8 9 10 11 |
/* * create a Gson object using the GsonBuilder * class and call serializeNulls methods * to include the null fields in JSON. */ Gson gson = new GsonBuilder() .serializeNulls() .create(); Employee emp2 = new Employee("Emp02", "John", 92000, null); System.out.println(gson.toJson(emp2)); |
Output
1 |
{"Id":"Emp02","Name":"John","Salary":92000.0,"Designation":null} |
As we can see, the Designation field is serialized even though it is specified as null.
Please let me know your views in the comments section below.