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:


Let’s begin with setting up the essentials. In order to use Gson API, we will have to add it as a dependency. Also, we will create a Java Pojo class that will be used during the examples.

Gson Dependency

Use latest version of Gson API.


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

Or, in build.gradle

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

Java POJO Class

We will create class to use it for the parsing of JSON strings.

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

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

Next 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 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)


In this short tutorial we have learnt 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