Gson Jackson To Map JSON To POJO And POJO To JSON

Introduction

This tutorial will show you how you can map JSON string to a list of POJO objects and a list of POJO objects to JSON string using Google API’s Gson as well as Jackson API’s ObjectMapper.

I will create nested POJO objects from JSON string or JSON string to nested POJO objects using both Gson and Jackson API.

Google Gson has the following features:

  1. Provides simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa
  2. Allows pre-existing unmodifiable objects to be converted to and from JSON
  3. Has extensive support of Java Generics
  4. Allows custom representations for objects
  5. Supports arbitrarily complex objects with deep inheritance hierarchies and extensive use of generic types

Jackson has the following features:

  1. Streaming API or incremental parsing/generation: reads and writes JSON content as discrete events
  2. Tree model: provides a mutable in-memory tree representation of a JSON document
  3. Data binding: converts JSON to and from POJO’s
  4. Some comparison blogs click here blogs1, blog2

Google Gson performs faster than Jackson irrespective of small or large objects.

Prerequisites

Java 1.8+, Maven 3.6.3 – 3.8.5/Gradle 6.4.1, Gson 2.8.6 – 2.9.0, Jackson 2.11.1 – 2.13.3

Project Setup

Create a gradle or maven based project in your favorite IDE or tool. The name of the project is java-json-object-or-object-json.

If you are creating gradle based project then use below build.gradle script:

plugins {
    id 'java-library'
}

repositories {
    jcenter()
}

dependencies {
    implementation('com.google.code.gson:gson:2.8.6')
    implementation('com.fasterxml.jackson.core:jackson-databind:2.11.1')
}

I am using both Gson and Jackson in the build file to show you conversion examples using Gson and Jackson.

If you are creating maven based project then use below pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>java-json-object-or-object-json</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>11</maven.compiler.source>
		<maven.compiler.target>11</maven.compiler.target>
	</properties>

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

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

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</plugin>
		</plugins>
	</build>
</project>

POJO Classes

POJO classes are required to convert from JSON string to Java object. I am creating nested POJO classes.

So A department may have multiple employees.

public class Employee {

	private int id;
	private String name;
	private String email;

	public Employee() {
	}

	public Employee(int id, String name, String email) {
		this.id = id;
		this.name = name;
		this.email = email;
	}

	//getters and setters

	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", email=" + email + "]";
	}

}

The following POJO class is Department.

public class Department {

	private int id;
	private String name;

	private List<Employee> employees;

	//getters and setters

	@Override
	public String toString() {
		return "Department [id=" + id + ", name=" + name + ", employees=" + employees + "]";
	}

}

JSON to Java Object using Gson

Now I will convert a JSON string to Java object using the following piece of code.

public class JsonToObjectUsingGoogleGson {

	public static void main(String[] args) {
		String json = "{\"id\":1, \"employees\":[{\"id\":1001,\"name\":\"Sumit\",\"email\":\"sumit.ghosh@email.com\"}, {\"id\":1002,\"name\":\"Soumitra\",\"email\":\"soumitra.sarkar@email.com\"}, {\"id\":1003,\"name\":\"Gourab\",\"email\":\"gourab.guha@email.com\"}, {\"id\":1004,\"name\":\"Debina\",\"email\":\"debina.guha@email.com\"}, {\"id\":1005,\"name\":\"Debabrata\",\"email\":\"debabrata.poddar@email.com\"}, {\"id\":1006,\"name\":\"Liton\",\"email\":\"liton.sarkar@email.com\"}, {\"id\":1007,\"name\":\"Souvik\",\"email\":\"souvik.sanyal@email.com\"}]}";

		Gson gson = new Gson();

		Department department = gson.fromJson(json, Department.class);

		System.out.println(department);
	}
}

Running the above example you will the following output:

Department [id=1, name=null, employees=[Employee [id=1001, name=Sumit, email=sumit.ghosh@email.com], Employee [id=1002, name=Soumitra, email=soumitra.sarkar@email.com], Employee [id=1003, name=Gourab, email=gourab.guha@email.com], Employee [id=1004, name=Debina, email=debina.guha@email.com], Employee [id=1005, name=Debabrata, email=debabrata.poddar@email.com], Employee [id=1006, name=Liton, email=liton.sarkar@email.com], Employee [id=1007, name=Souvik, email=souvik.sanyal@email.com]]]

Java Object to JSOn using Gson

Next I will convert Java object to JSON string using the following code.

Here I am not creating Employee or Department objects, rather I am first converting the JSON string to POJO then back to JSON.

public class ObjectToJsonUsingGoogleGson {

	public static void main(String[] args) {
		String json = "{\"id\":1, \"employees\":[{\"id\":1001,\"name\":\"Sumit\",\"email\":\"sumit.ghosh@email.com\"}, {\"id\":1002,\"name\":\"Soumitra\",\"email\":\"soumitra.sarkar@email.com\"}, {\"id\":1003,\"name\":\"Gourab\",\"email\":\"gourab.guha@email.com\"}, {\"id\":1004,\"name\":\"Debina\",\"email\":\"debina.guha@email.com\"}, {\"id\":1005,\"name\":\"Debabrata\",\"email\":\"debabrata.poddar@email.com\"}, {\"id\":1006,\"name\":\"Liton\",\"email\":\"liton.sarkar@email.com\"}, {\"id\":1007,\"name\":\"Souvik\",\"email\":\"souvik.sanyal@email.com\"}]}";

		Gson gson = new Gson();

		Department department = gson.fromJson(json, Department.class);

		String outputJson = gson.toJson(department);

		System.out.println(outputJson);
	}

}

Running the above code you will get the following output:

{"id":1,"employees":[{"id":1001,"name":"Sumit","email":"sumit.ghosh@email.com"},{"id":1002,"name":"Soumitra","email":"soumitra.sarkar@email.com"},{"id":1003,"name":"Gourab","email":"gourab.guha@email.com"},{"id":1004,"name":"Debina","email":"debina.guha@email.com"},{"id":1005,"name":"Debabrata","email":"debabrata.poddar@email.com"},{"id":1006,"name":"Liton","email":"liton.sarkar@email.com"},{"id":1007,"name":"Souvik","email":"souvik.sanyal@email.com"}]}

JSON to Java Object using Jackson

You have seen how to convert JSON to Java object using Google gson, now I will do the similar thing using Jackson API.

public class JsonToObjectUsingJackson {

	public static void main(String[] args) {
		String json = "{\"id\":1, \"employees\":[{\"id\":1001,\"name\":\"Sumit\",\"email\":\"sumit.ghosh@email.com\"}, {\"id\":1002,\"name\":\"Soumitra\",\"email\":\"soumitra.sarkar@email.com\"}, {\"id\":1003,\"name\":\"Gourab\",\"email\":\"gourab.guha@email.com\"}, {\"id\":1004,\"name\":\"Debina\",\"email\":\"debina.guha@email.com\"}, {\"id\":1005,\"name\":\"Debabrata\",\"email\":\"debabrata.poddar@email.com\"}, {\"id\":1006,\"name\":\"Liton\",\"email\":\"liton.sarkar@email.com\"}, {\"id\":1007,\"name\":\"Souvik\",\"email\":\"souvik.sanyal@email.com\"}]}";

		ObjectMapper mapper = new ObjectMapper();

		Department department = null;
		try {
			department = mapper.readValue(json, Department.class);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}

		System.out.println(department);
	}
}

Running the above code you will see the following output:

Department [id=1, name=null, employees=[Employee [id=1001, name=Sumit, email=sumit.ghosh@email.com], Employee [id=1002, name=Soumitra, email=soumitra.sarkar@email.com], Employee [id=1003, name=Gourab, email=gourab.guha@email.com], Employee [id=1004, name=Debina, email=debina.guha@email.com], Employee [id=1005, name=Debabrata, email=debabrata.poddar@email.com], Employee [id=1006, name=Liton, email=liton.sarkar@email.com], Employee [id=1007, name=Souvik, email=souvik.sanyal@email.com]]]

Java Object to JSON using Jackson

Now again I will convert java object to JSON string using Jackson API as I did using Google Gson API.

public class ObjectToJsonUsingJackson {

	public static void main(String[] args) {
		String json = "{\"id\":1, \"employees\":[{\"id\":1001,\"name\":\"Sumit\",\"email\":\"sumit.ghosh@email.com\"}, {\"id\":1002,\"name\":\"Soumitra\",\"email\":\"soumitra.sarkar@email.com\"}, {\"id\":1003,\"name\":\"Gourab\",\"email\":\"gourab.guha@email.com\"}, {\"id\":1004,\"name\":\"Debina\",\"email\":\"debina.guha@email.com\"}, {\"id\":1005,\"name\":\"Debabrata\",\"email\":\"debabrata.poddar@email.com\"}, {\"id\":1006,\"name\":\"Liton\",\"email\":\"liton.sarkar@email.com\"}, {\"id\":1007,\"name\":\"Souvik\",\"email\":\"souvik.sanyal@email.com\"}]}";

		ObjectMapper mapper = new ObjectMapper();

		String outputJson = null;
		try {
			Department department = mapper.readValue(json, Department.class);
			outputJson = mapper.writeValueAsString(department);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}

		System.out.println(outputJson);
	}

}

Running the above code you will see the following output:

{"id":1,"name":null,"employees":[{"id":1001,"name":"Sumit","email":"sumit.ghosh@email.com"},{"id":1002,"name":"Soumitra","email":"soumitra.sarkar@email.com"},{"id":1003,"name":"Gourab","email":"gourab.guha@email.com"},{"id":1004,"name":"Debina","email":"debina.guha@email.com"},{"id":1005,"name":"Debabrata","email":"debabrata.poddar@email.com"},{"id":1006,"name":"Liton","email":"liton.sarkar@email.com"},{"id":1007,"name":"Souvik","email":"souvik.sanyal@email.com"}]}

That’s all about conversion of JSON to Java and Java to JSON using Google Gson and Jackson APIs.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *