Over a million developers have joined DZone.

Deserializing Json to a Java Object Using Google’s Gson Library

DZone's Guide to

Deserializing Json to a Java Object Using Google’s Gson Library

· Java Zone ·
Free Resource

Atomist automates your software deliver experience. It's how modern teams deliver modern software.

Javascript object notation (JSON) is fast becoming the de facto standard or format for transferring, sharing and passing around data. Be it on the web, REST Service, a remote procedure call or even an ajax request. JSON is light weight with little memory footprint when compared to an xml. 

The content of a JSON string in its raw form when observed looks gibberish. To make the content usable it needs to be deserialized or converted to a useable form usually a Java object (POJO) or an array or list of objects depending on the JSON content. 

A typical JSON string is as shown below 

{"city":"Jos","country":"Nigeria","houseNumber":"13","lga":"Jos South",
         "state":"Plateau","streetName":"Jonah Jann","village":"Bukuru","ward":"1"}

There are a lot of frameworks for deserializing json to a java object such as  json-rpcGsonFlexjson and a whole lots of other open source libraries. Of all the libraries mentioned I would in this blog post demonstrate how to use google-Gson library to deserialize a json string to a java object. You can download the  Gson library from https://code.google.com/p/google-gson/

To have the json string deserialized, a java object must be created that has the same fields names with the fields in the json string. There is a website that provides a service for viewing the content of a json string in a tree like manner.  http://jsonviewer.stack.hu 

Paste the json string in the text tab 

And view the fields and the content from the Viewer tab 

I would deserialize a json string that contains address details to an Address POJO, the address object follows the structure as seen from the json tree view above. 

public class Address{

private String city;

private String country;

private String houseNumber;

private String lga;

private String state;

private String streetName;

private String village;

private String ward;

public String getCity() {
return city;

public void setCity(String city) {
this.city = city;

public String getCountry() {
return country;

public void setCountry(String country) {
this.country = country;

public String getHouseNumber() {
return houseNumber;

public void setHouseNumber(String houseNumber) {
this.houseNumber = houseNumber;

public String getLga() {
return lga;

public void setLga(String lga) {
this.lga = lga;

public String getState() {
return state;

public void setState(String state) {
this.state = state;

public String getStreetName() {
return streetName;

public void setStreetName(String streetName) {
this.streetName = streetName;
public String getVillage() {
return village;

public void setVillage(String village) {
this.village = village;

public String getWard() {
return ward;

public void setWard(String ward) {
this.ward = ward;

public String toString() {
return "Address [city=" + city + ", country=" + country
+ ", houseNumber=" + houseNumber + ", lga=" + lga + ", state="
+ state + ", streetName=" + streetName + ", village=" + village
+ ", ward=" + ward + "]";

To perform the deserialization with Gson is easy, create POJO classes to hold your data, import the packages com.google.gson.Gson and com.google.gson.GsonBuilder, to your project. Then create and instance of the Gson class and then perform the deserialization as shown below.
Gson gson = new GsonBuilder().create();
Address address=gson.fromJson(json, Address.class);

Voila, you have your json deserialized! 

The source code listing is below. 

package jsondeserializer

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

  public class Tester {
public static void main(String[] args) {
String json ="{\"city\":\"Jos\",\"country\":\"Nigeria\",\"houseNumber\":\"13\",\"lga\":\"Jos South\",\n" + 
"\"state\":\"Plateau\",\"streetName\":\"Jonah Jann\",\"village\":\"Bukuru\",\"ward\":\"1\"}";
Gson gson = new GsonBuilder().create();
Address address=gson.fromJson(json, Address.class);

Get the open source Atomist Software Delivery Machine and start automating your delivery right there on your own laptop, today!

java ,tips and tricks

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}