5 ways to convert JSON to Java Object - Using Jackson, Gson, Genson, Moshi, & FastJson

Hello everyone, today we will show you how to convert JSON to Java Object using five different Java libraries which are listed below.

1. Jackson
2. Gson
3. Moshi
4. Genson
5 FastJson

In this example, we are going to convert the below JSON string to Java Object,

Sample JSON String


public class Json {

public static String json = ""
+ "{\"isbn\": \"123-456-222\", \n"
+ " \"author\": \n"
+ " {\n"
+ " \"lastname\": \"Doe\",\n"
+ " \"firstname\": \"Jane\"\n"
+ " },\n"
+ "\"editor\": \n"
+ " {\n"
+ " \"lastname\": \"Smith\",\n"
+ " \"firstname\": \"Jane\"\n"
+ " },\n"
+ " \"title\": \"The Ultimate Database Study Guide\", \n"
+ " \"category\": [\"Non-Fiction\", \"Technology\"]\n"
+ " }";
}



We'll use the following Root class with multiple fields as the object to deserialize throughout.

"Remember - The POJOs in java are useful in defining objects to increase their readability and reusability."

Root.java

import java.util.List;

public class Root {
public String isbn;
public Author author;
public Editor editor;
public String title;
public List<String> category;

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public Author getAuthor() {
return author;
}

public void setAuthor(Author author) {
this.author = author;
}

public Editor getEditor() {
return editor;
}

public void setEditor(Editor editor) {
this.editor = editor;
}

public String getTitle() {
return title;
}

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

public List<String> getCategory() {
return category;
}

public void setCategory(List<String> category) {
this.category = category;
}

@Override
public String toString() {
return "Root [isbn=" + isbn + ", author=" +
author + ", editor=" + editor +
", title=" + title + ", category="
+ category + "]";
}

public Root() {
super();
}

public Root(String isbn, Author author,
Editor editor, String title,
List<String> category) {
super();
this.isbn = isbn;
this.author = author;
this.editor = editor;
this.title = title;
this.category = category;
}
}


Editor.java

public class Editor {

public String lastname;
public String firstname;

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public Editor(String lastname, String firstname) {
super();
this.lastname = lastname;
this.firstname = firstname;
}

public Editor() {
super();
}

@Override
public String toString() {
return "Editor [lastname=" + lastname
+ ", firstname=" + firstname + "]";
}
}


Author.java

public class Author {
public String lastname;
public String firstname;

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public Author(String lastname, String firstname) {
super();
this.lastname = lastname;
this.firstname = firstname;
}

public Author() {
super();
}

@Override
public String toString() {
return "Author [lastname=" + lastname
+ ", firstname=" + firstname + "]";
}
}


If you want to use Jackson add the following dependencies:

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>


If you want to use Genson add the following dependencies:

<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.6</version>
</dependency>



If you want to use Moshi add the following dependencies:

<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi-adapters</artifactId>
<version>1.9.2</version>
</dependency>



If you want to use FastJson add the following dependencies:

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>



If you want to use Gson add the following dependencies:

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


Print the values on the system console and verify the results,

import java.io.IOException;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.owlike.genson.Genson;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;

public class DemoApplication {

public static void main(String[] args) throws IOException {

// Using Jackson
ObjectMapper objectMapper = new ObjectMapper();
Root root1 = objectMapper.readValue(Json.json, Root.class);
System.out.println(root1);

// Using Moshi
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Root> jsonAdapter = moshi.adapter(Root.class);
Root root2 = jsonAdapter.fromJson(Json.json);
System.out.println(root2);

// Using Gson
Gson gson = new Gson();
Root root3 = gson.fromJson(Json.json, Root.class);
System.out.println(root3);

// Using Genson
Root root4 = new Genson().deserialize(Json.json, Root.class);
System.out.println(root4);

// Using Fast JSON
Root root5 = JSON.parseObject(Json.json, Root.class);
System.out.println(root5);
}
}


Console Output:

Root [isbn=123-456-222, author=Author [lastname=Doe, firstname=Jane], editor=Editor [lastname=Smith, firstname=Jane], title=The Ultimate Database Study Guide, category=[Non-Fiction, Technology]]
Root [isbn=123-456-222, author=Author [lastname=Doe, firstname=Jane], editor=Editor [lastname=Smith, firstname=Jane], title=The Ultimate Database Study Guide, category=[Non-Fiction, Technology]]
Root [isbn=123-456-222, author=Author [lastname=Doe, firstname=Jane], editor=Editor [lastname=Smith, firstname=Jane], title=The Ultimate Database Study Guide, category=[Non-Fiction, Technology]]
Root [isbn=123-456-222, author=Author [lastname=Doe, firstname=Jane], editor=Editor [lastname=Smith, firstname=Jane], title=The Ultimate Database Study Guide, category=[Non-Fiction, Technology]]
Root [isbn=123-456-222, author=Author [lastname=Doe, firstname=Jane], editor=Editor [lastname=Smith, firstname=Jane], title=The Ultimate Database Study Guide, category=[Non-Fiction, Technology]]

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