Parse JSON Strings in Java Objects with Gson API

This tutorial covers How to Parse JSON in Java. You will learn to use Gson API to convert JSON Strings into Java Objects with the help of examples.

This tutorial is limited to Using Gson API to Converting Java Objects to JSON.
To Learn more:

Setup

Let’s begin with setting up the essentials. To use Gson API, we will have to add it as a dependency. Also, we will create a Java Pojo class that will be our target.

Gson Dependency

Use the latest version of Gson API.

pom.xml

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

Or, in build.gradle

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

Java POJO Class

We will create the Account.java class.

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

public class Account {
    private long accountNumber;
    private String accountHolder;
    private String type;
    private double balance;

    public long getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(long accountNumber) {
        this.accountNumber = accountNumber;
    }

    public String getAccountHolder() {
        return accountHolder;
    }

    public void setAccountHolder(String accountHolder) {
        this.accountHolder = accountHolder;
    }

    public String getType() {
        return type;
    }

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

    public double getBalance() {
        return balance;
    }

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

JSON to Object using Gson

Let’s try an example of using Gson to Parse JSON String into an Object.

String json = """
        {
            "accountNumber":1234,
            "accountHolder":"Strong Belwas",
            "type":"Savings",
            "balance":1239.39
        }
        """;
Gson gson = new Gson();
Account account = gson.fromJson(json, Account.class);
System.out.println(account);Code language: Java (java)

Here, we are using Java Text Blocks to hold multiline JSON String.

JSON to Map using Gson

To convert JSON into a Map, which has a generic type, we need to create a Type instance.

The following example demonstrates using Gson to parse JSON string to a Map.

String json = """
        {
            "accountNumber":1234,
            "accountHolder":"Strong Belwas",
            "type":"Savings",
            "balance":1239.39
        }
        """;
Gson gson = new Gson();
Type type = new TypeToken<Map<String, String>>() {}.getType();
Map<String, String> account = gson.fromJson(json, type);Code language: Java (java)

JSON to Map of Objects using Gson

Similarly, we can cast a JSON to Map of custom objects by using the correct Type.

String json = """
        {
            "1234":{
                "accountNumber":1234,
                "accountHolder":"Strong Belwas",
                "type":"Savings",
                "balance":1239.39
            }
        }
        """;
Gson gson = new Gson();
Type type = new TypeToken<Map<Long, Account>>() {}.getType();
Map<Long, Account> account = gson.fromJson(json, type);Code language: Java (java)

JSON to Array of Objects using Gson

The JSON String is an array of Students we can map it to Student[].

String json = """
        [{
            "accountNumber":1234,
            "accountHolder":"Strong Belwas",
            "type":"Savings",
            "balance":1239.39
        }]
        """;
Gson gson = new Gson();
Student[] students = gson.fromJson(json, Student[].class);Code language: Java (java)

Summary

In this short tutorial, we have learned How to use Gson API. We have covered examples to parse JSON Strings into Java Objects, Maps, and Arrays.
For more on Java, please visit Java Tutorials.