Home  >  Google API

Java Gson + JSON Tutorial with Examples

By Arvind Rai, July 21, 2014
Gson is google API that effectively coverts JSON into corresponding java object and java object into JSON string. Gson provides options to print JSON in well-defined way. In our example I will provide a complete demo how to use Gson. We will see how to create Gson object and how API can be used to convert between java object and JSON.

Gson Jar to Resolve Dependency

Resolve project dependency either by directly downloading gson-2.2.4.jar or use maven as below.

Ways To Create GSON Object

There are two way to create Gson object.

1. This is the simple way and we get all default settings for Gson.
Gson gson = new Gson(); 
2. GsonBuilder is also used to get instance of Gson that facilitate to set other settings. Suppose we want pretty printing of Json, then we can create Gson object as below.
Gson gson = new GsonBuilder().setPrettyPrinting().create(); 

From JSON to Java Object Using GSON API

Gson has fromJson() method that accepts JSON string and a class. This method converts JSON into the object of specified class. In our example we have a person data as JSON and person class. Find the JSON string which we will use to create Peron object.
Person JSON
{  'id': 10,
   'name': 'Ram', 
    {'city': 'Varanasi',
     'zip': 221001
     [ 111111, 
Find the Address class used in Person class.
package com.concretepage.gson;
public class Address {
    private String city;
    private String zip;
	public String getCity() {
		return city;
	public void setCity(String city) {
		this.city = city;
	public String getZip() {
		return zip;
	public void setZip(String zip) {
		this.zip = zip;
Now find the person clas. Person class has all fields for person JSON. Person class field name and JSON key name must match so that Gson can find the field name to set the value from JSON.
package com.concretepage.gson;
public class Person {
    private int id;
    private String name;
    private Address address;
    private long[] mobileNums;
	public int getId() {
		return id;
	public void setId(int id) {
		this.id = id;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;
	public Address getAddress() {
		return address;
	public void setAddress(Address address) {
		this.address = address;
	public long[] getMobileNums() {
		return mobileNums;
	public void setMobileNums(long[] mobileNums) {
		this.mobileNums = mobileNums;
Now find the main class which will use Gson API to convert JSON into person Object.
package com.concretepage.gson;
import com.google.gson.Gson;
public class JsonToJavaObject {
  public static void main(String[] args) {
	  String jsonStr= "{  'id': 10, 'name': 'Ram', 'address': {'city': 'Varanasi','zip':" +
	  		" 221001 },'mobileNums': [ 111111, 222222 ]}";
	  Gson gson = new Gson();
	  Person person = gson.fromJson(jsonStr, Person.class);
	  long mobNums[] = person.getMobileNums();
Now person object has all data which been declared in JSON format. Find the output for some person field.

From Java Object to JSON using GSON API

Gson has toJson() method that accepts an object and returns the corresponding JSON string. In our example we will create a person object and then we will print its JSON format.
package com.concretepage.gson;
import com.google.gson.Gson;
public class JavaObjectToJson {
  private static void createPerson(Person person){
	  Address address = new Address();
	  long[] mobNums = {33333,44444};
  public static void main(String[] args) {
	  Person person = new Person();
	  Gson gson = new Gson();
Find the output as JSON from person java object.

Pretty Printing for JSON Using Gson

When we create instance of Gson using GsonBuilder , we can set different format settings. Gson by default prints compact output of JSON which will be in one line. We can set Pretty Printing and the Gson will print JSON in well formatted. Change main method as below in JavaObjectToJson.java
	  public static void main(String[] args) {
		  Person person = new Person();
		  Gson gson = new GsonBuilder().setPrettyPrinting().create();
		  String jsonStr = gson.toJson(person);
Find JSON pretty printing from person java object.
  "id": 15,
  "name": "Shyam",
  "address": {
    "city": "Allahabad",
    "zip": "230043"
  "mobileNums": [

©2020 concretepage.com | Privacy Policy | Contact Us