How to convert object to / from JSON using Gson

  • 15 June 2016
  • ADM


How to convert object to / from JSON using Gson - images/logos/java.jpg


Here is a simple example of converting objects into JSON strings and back using Gson.


Download and add the jar file as dependency for your project.

For Maven users add the Gson dependency in your pom.xml file



For this tutorials I will use 2 POJO classes: User and Permission and one class as test.

One simple class:

package com.admfactory.gson;

public class Permission {
	private String name;
	public Permission(String name) { = name;

	public String getName() {
		return name;

	public void setName(String name) { = name;
} will use as embedded object.

package com.admfactory.gson;

import java.util.ArrayList;
import java.util.List;

public class User {
	private String username;
	private String email;
	private String password;
	private List<Permission> permissions;
	public String getUsername() {
		return username;
	public void setUsername(String username) {
		this.username = username;
	public String getEmail() {
		return email;
	public void setEmail(String email) { = email;
	public String getPassword() {
		return password;
	public void setPassword(String password) {
		this.password = password;
	public List<Permission> getPermissions() {
		return permissions;
	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	 * Method used to add a permission to the array. 
	 * @param p {@link Permission} object
	public void addPermission(Permission p) {
		/** Check if the array is null, if yes create a new array */
		if (this.permissions == null)
			this.permissions = new ArrayList<Permission>();
} used as test class.

package com.admfactory.gson;


public class GsonTest {
	public static void main(String[] args) {
		/** Create the gson builder object */
		 GsonBuilder builder = new GsonBuilder();
       Gson gson = builder.create();
       System.out.println("Object to JSON String Test");
       /** Create a user object */
       User user = new User();
       /** Add some permission objects as embedded objects */
       Permission p1 = new Permission("add.user");
       Permission p2 = new Permission("delete.user");
       Permission p3 = new Permission("modify.user");
       /** Get the string from the object and print it on the console */
       String string = gson.toJson(user);
       System.out.println("JSON String: " + string);
       System.out.println("JSON String to Object Test");
       /** Use the string printed to create a new object */
       User u = gson.fromJson(string, User.class);
       /** Print one variable to the console */
       System.out.println("Username : " + u.getUsername());
       /** Print one embedded variable to the console */
       System.out.println("First permission: " + u.getPermissions().get(0).getName());

For more details please follow the comments from the source code.


Object to JSON String Test
JSON String: {"username":"johndoe","email":"","password":"a1234","permissions":[{"name":"add.user"},{"name":"delete.user"},{"name":"modify.user"}]}

JSON String to Object Test
Username : johndoe
First permission: add.user


As you can see the string output is on a single line. If you need to have a pretty print for your json string replace the Gson object creating line from

/** Create the gson builder object */
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();


/** Create the gson builder object */
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.setPrettyPrinting().create();

And now the output will be in a human readable format.

JSON String: {
  "username": "johndoe",
  "email": "",
  "password": "a1234",
  "permissions": [
      "name": "add.user"
      "name": "delete.user"
      "name": "modify.user"