Convert object to/from json using Gson

Convert object to/from json using Gson

Google GSON is JSON parser and also generator for java, which is widely use to convert object to/from Json. To learn more about Gson one can refer to Gson tutorial. GSON library is used to convert any object to Json and vice-versa. Here in this tutorial we’ll be discussing below popular examples to convert different object to/from Json.

  1. Convert Object to/from Json.
  2. Convert List of objects to/from Json.
  3. Convert Map containing objects.

Simple POJO which will be used as an object in all examples.

public class Emp {
	private int id;
	private String firstName;
	private String lastName;
 
	public Emp() {}
 
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Id : " + id);
		sb.append(", FirstName : " + firstName);
		sb.append(", LastName : " + lastName);
 
		return sb.toString();
	}
}

1) Convert Object to/from Json

  • Example to convert Emp object to Json.
    Emp emp = new Emp();
    emp.setId(1);
    emp.setFirstName("Rahul");
    emp.setLastName("Anand");
    		
    Gson gson = new Gson();
    String jsonString = gson.toJson(emp);
    System.out.println(jsonString);
    Output
    {"id":1,"firstName":"Rahul","lastName":"Anand"}
  • Example to convert Json to Emp object.
    String jsonString = "{\"id\":1,\"firstName\":\"Rahul\",\"lastName\":\"Anand\"}";
    Gson gson = new Gson();
    Emp emp = gson.fromJson(jsonString, Emp.class);
    System.out.println(emp);
    Output
    Id : 1, FirstName : Rahul, LastName : Anand

2) Convert List of objects to/from Json

  • Example to convert List of Emp objects to Json.
    List<Emp> employees = new ArrayList<Emp>(); 
    Emp emp1 = new Emp();
    emp1.setId(1);
    emp1.setFirstName("Rahul");
    emp1.setLastName("Anand");
    		
    Emp emp2 = new Emp();
    emp2.setId(2);
    emp2.setFirstName("Daniel");
    emp2.setLastName("Anderson");
    		
    employees.add(emp1);
    employees.add(emp2);
    		
    Gson gson = new Gson();
    String jsonString = gson.toJson(employees);
    System.out.println(jsonString);
    Output
    [{"id":1,"firstName":"Rahul","lastName":"Anand"},{"id":2,"firstName":"Daniel","lastName":"Anderson"}]
  • Example to convert Json to List of Emp objects.
    String jsonString = "[{\"id\":1,\"firstName\":\"Rahul\",\"lastName\":\"Anand\"},{\"id\":2,\"firstName\":\"Daniel\",\"lastName\":\"Anderson\"}]";
    Gson gson = new Gson();
    List<Emp> employees = gson.fromJson(jsonString, new TypeToken<List<Emp>>(){}.getType());
    System.out.println(employees);

    Json object in user readable format.

    [
      {
        "id": 1,
        "firstName": "Rahul",
        "lastName": "Anand"
      },
      {
        "id": 2,
        "firstName": "Daniel",
        "lastName": "Anderson"
      }
    ]
    
    Output
    [Id : 1, FirstName : Rahul, LastName : Anand, Id : 2, FirstName : Daniel, LastName : Anderson]

3) Convert Map containing objects.

  • Example to convert Map containing Emp objects to Json.
    Map<Integer, Emp> employees = new HashMap<Integer, Emp>(); 
    Emp emp1 = new Emp();
    emp1.setId(1);
    emp1.setFirstName("Rahul");
    emp1.setLastName("Anand");
    		
    Emp emp2 = new Emp();
    emp2.setId(2);
    emp2.setFirstName("Daniel");
    emp2.setLastName("Anderson");
    		
    employees.put(1, emp1);
    employees.put(2, emp2);
    		
    Gson gson = new Gson();
    String jsonString = gson.toJson(employees);
    System.out.println(jsonString);
    Output
    {"1":{"id":1,"firstName":"Rahul","lastName":"Anand"},"2":{"id":2,"firstName":"Daniel","lastName":"Anderson"}}
  • Example to convert Json to Map.
    String jsonString = "{\"1\":{\"id\":1,\"firstName\":\"Rahul\",\"lastName\":\"Anand\"},\"2\":{\"id\":2,\"firstName\":\"Daniel\",\"lastName\":\"Anderson\"}}";
    Gson gson = new Gson();
    Type type = new TypeToken<Map<Integer, Emp>>(){}.getType();
    Map<Integer, Emp> employees = gson.fromJson(jsonString, type);
    System.out.println(employees);

    Json String in user readable format

    {
      "1": {
        "id": 1,
        "firstName": "Rahul",
        "lastName": "Anand"
      },
      "2": {
        "id": 2,
        "firstName": "Daniel",
        "lastName": "Anderson"
      }
    }
    
    Output
    {1=Id : 1, FirstName : Rahul, Last Name : Anand, 2=Id : 2, FirstName : Daniel, Last Name : Anderson}

    Stay tuned for more tutorials and examples.

Leave a Reply

Your email address will not be published. Required fields are marked *