How to convert nested Json string to Java object using Gson library

Json is everywhere in back end programming, from various webservices, databases and so on.  Gson library is the one the most available libraries for Java which makes the conversion of Json string to Java object hussle free and very easy. What do you all need to do is to download this library and add it to your Java project and then create a class for your Json string in Java and map values one by one. Then using Gson convert the Json string to Java object. That’s it.

In order to understand the situation better, let’s follow a real world example.

Assume we want to convert the result of Weather forecast web-service from Json into Java object.

The result in Json format is look like below,

"name":"New York",

As you can see, the result contains nested Json and somehow array/list. Don’t worry, conversion for this case is quite easy as well.

Now, create a class in your Java project (e.g. class name : and map the variable one by one from Json string result in your Java object like the following,

import java.math.BigDecimal;
import java.util.List;
public class WebServiceResponseJson {
public Coord coord;

public List<Weather> weather;

public String base;

public Main main;

public BigDecimal visibility;

public Wind wind;

public Clouds clouds;

public BigDecimal dt;

public Sys sys;

public BigDecimal id;

public String name;

public BigDecimal cod;

public static class Coord {
public BigDecimal lon;
public BigDecimal lat;
public Coord() {
this.lon = new BigDecimal(0); = new BigDecimal(0);
public static class Weather {
public Integer id;
public String main;
public String description;
public String icon;
public Weather() { = new Integer(0);
this.main = "";
this.description = "";
this.icon = "";
public static class Main {
public BigDecimal temp;
public BigDecimal pressure;
public BigDecimal humidity;
public BigDecimal temp_min;
public BigDecimal temp_max;
public Main() {
this.temp = new BigDecimal(0);
this.pressure = new BigDecimal(0);
this.humidity = new BigDecimal(0);
this.temp_min = new BigDecimal(0);
this.temp_max = new BigDecimal(0);
public static class Wind {
public BigDecimal speed;
public BigDecimal deg;
public Wind() {
this.speed = new BigDecimal(0);
this.deg = new BigDecimal(0);
public static class Clouds {
public BigDecimal all;
public Clouds() {
this.all = new BigDecimal(0);
public static class Sys {
public BigDecimal type;
public BigDecimal id;
public BigDecimal message;
public String country;
public BigDecimal sunrise;
public BigDecimal sunset;
public Sys() {
this.type = new BigDecimal(0); = new BigDecimal(0);
this.message = new BigDecimal(0); = "";
this.sunrise = new BigDecimal(0);
this.sunset = new BigDecimal(0);

As you can see for nested values, I have defined an inner class to handle the situation and then use this class in either plane form or in form of list (to handle Json array).

Now let’s move to the next part. After you called your webservice, you will get the result in Json string form, now, you just need to convert it to Java object. In order to do that, you should use Gson library like below,

private WebServiceResponseJson parseJsonObj(String jsonString) {
WebServiceResponseJson result = null;
try {
Gson gson = new Gson();
result = gson.fromJson(jsonString,WebServiceResponseJson.class);
} catch(Exception ex) {

After that you can use the result of your webservice call easily via result class. For better understand you can have a look to this example in Github.

Categories: Programming

Leave a Reply