How to convert Java object to JSON and JSON to Java object with Gson?

In this section, we will show you how to convert Java object to JSON and JSON to Java object with Gson.

Note


Google Gson is a simple Java-based library to serialize Java objects to JSON and vice versa. It is an open-source library developed by Google.

Note


JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging.

JSON is built on two structures: 

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. 
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

1. Download Gson

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>

2. Java Objects to JSON

A User POJO, later uses this for conversion.

User.java

public class User {

private Long id;
private String name;

public User(Long id, String name) {
this.id = id;
this.name = name;
}

public User() {
}

public Long getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}


In Gson, we can use gson.toJson() to convert Java objects to JSON.

Main.java

import com.google.gson.Gson;
import java.util.Arrays;
import java.util.List;

public class Main {

public static void main(String[] args) {

Gson gson = new Gson();

User user1 = new User(1l,"Sibin");
User user2 = new User(2l,"Tom");
User user3 = new User(3l,"John");

List<User> users = Arrays.asList(user1,user2,user3);

// Java objects to String
String json = gson.toJson(users);
System.out.println(json);
}
}

Console Output:
[{"id":1,"name":"Sibin"},{"id":2,"name":"Tom"},{"id":3,"name":"John"}]

3. JSON to Java Objects

In Gson, we can use gson.fromJson to convert JSON back to Java objects.

Main.java

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.List;

public class Main {

public static void main(String[] args) {

Gson gson = new Gson();

String json = "[{\"id\":1,\"name\":\"Sibin\"}" +
",{\"id\":2,\"name\":\"Tom\"}" +
",{\"id\":3,\"name\":\"John\"}]";

List<User> users = gson.fromJson(json,
new TypeToken<List<User>>() {}.getType());
users.forEach(System.out::println);
}
}

Console Output:
User{id='1', name='Sibin'}
User{id='2', name='Tom'}
User{id='3', name='John'}

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete