Convert Java Objects To JSON with Gson API

A guide on How to use Gson API to Serialise Java Objects into JSON Strings. Covers examples of Converting Objects and Maps to JSON.

Setup

Before we try our examples, we will add Gson dependency and create a POJO class.

Gson Dependency

pom.xml

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>{version}</version>
</dependency>Code language: HTML, XML (xml)

Or build.gradle

compile group: 'com.google.code.gson', name: 'gson', version: '{version}<version>'</version>Code language: Gradle (gradle)

Make sure you are using the latest version of the Gson library.

POJO Class

Product.java

package com.amitph.spring.tutorials.students.model;

public class Product {
    private long id;
    private String title;
    private String type;
    private double price;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}Code language: Java (java)

Custom Object to JSON using Gson

We are using toJson method on the Gson instance to convert Java POJO into JSON String.

Product product = new Product();
product.setId(1234L);
product.setTitle("Lx Printer");
product.setType("Computer Accessories");
product.setPrice(299.9);

Gson gson = new Gson();

String json = gson.toJson(product);Code language: Java (java)

When we print the JSON string, the serialized Product instance looks like this:

{"id":1234,"title":"Lx Printer","type":"Computer Accessories","price":299.9}
Code language: JSON / JSON with Comments (json)

Java Map to JSON using Gson

Next, we will convert a Java Map into JSON String.

Map<String, String> map = Map.of("one", "1", "two", "2");

Gson gson = new Gson();
String json = gson.toJson(map);Code language: Java (java)

Map of Custom Objects to JSON using Gson

Now, we will see an example of Converting Map<Long, Product> to a JSON String.

Map<Long, Product> map = Map.of(product.getId(), product);

Gson gson = new Gson();
String json = gson.toJson(map);Code language: Java (java)

Array of Objects to JSON using Gson

Next, Gson can transform an array of objects into a JSON string

Product[] products = new Product[]{product};
Gson gson = new Gson();
String json = gson.toJson(products);

System.out.println(json);Code language: Java (java)

We can see in the output that the output JSON is an array

[{"id":1234,"title":"Lx Printer","type":"Computer Accessories","price":299.9}]
Code language: JSON / JSON with Comments (json)

Collection of Custom Objects to JSON using Gson

Similarly, we can transform a Collection of Objects into JSON String

Collection<Product> products = List.of(product);
Gson gson = new Gson();
String json = gson.toJson(products);Code language: Java (java)

Summary

In this example-oriented tutorial, we learned How to use Gson API to transform objects into JSON. We covered examples of converting Java POJOs, Maps, Arrays, and Collections into JSON format.

For more on Java, please visit Java Tutorials.