Deserialise JSON to Java Object using GSON

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

In this tutorial we will show you how you can deserialise JSON to Java Object using GSON.

Maven dependencies

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""




Object to be Deserialised

package com.memorynotfound.json;

import java.util.Date;

public class Product {

    private long id;
    private String name;
    private Date created;
    private double amount;

    public Product(long id, String name, Date created, double amount) { = id; = name;
        this.created = created;
        this.amount = amount;

    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", created=" + created +
                ", amount=" + amount +

Serialise Java Object to JSON using GSON

package com.memorynotfound.json;


public class ConvertJsonToObject {

    public static void main(String... args){
        String json = "{\"id\":1,\"name\":\"Playstation 4\",\"created\":\"Dec 26, 2014 1:58:32 PM\",\"amount\":499.99}";
        Gson gson = new GsonBuilder().create();
        Product result = gson.fromJson(json, Product.class);


Product{id=1, name='Playstation 4', created=Fri Dec 26 13:58:32 CET 2014, amount=499.99}


You may also like...