How to create a Java Object from a JSON object

Benjamin Pourian
4 min readOct 25, 2018

I do love Java really but I couldn’t resist this GIPHY…..

This will be a very brief guide to creating a Java object from a JSON object using the popular gson` google library.

I will first demonstrate a simple JSON → POJO example then follow that with another example where I will be using annotations provided by gson to instantiate a new object by capturing the relevant serialized name.

Find the full working example here Github.

I am assuming you will have already setup a project using Maven or Gradle. You will need the following dependency added to your project accordingly;

Maven

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

Gradle

dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}

I have kept the examples very simple and easy to follow. Hopefully it should give you an overall idea of how powerful this library is.

JSON to Simple POJO example

Create a class called SimplePojo with the following fields and methods;

This is the path to the SimplePojo class in my project;

./json-to-java-object/src/main/java/com/jtjo/model/SimplePojo.java;

To best demonstrate the workings of the gson library I have written a bunch of unit tests using JUNIT5 .

Now that we have our SimplePojo class we can create a bunch of objects using a JSON object;

I have a class in my test directory called SimplePojoTest and this is the outline of the class as follows;

package com.jtjo.model;import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("SimplePojo Class - demonstrating the use of GSON")
class SimplePojoTest {
private SimplePojo actual;
private Gson gson;
private JsonObject jsonObject;

@BeforeEach
public void beforeEach(){
gson = new Gson();
jsonObject = new JsonObject();
}
@Test
@DisplayName("Simple Json converted into Java object")
public void jsonToObjectOne(){
/* This is where our test will live **/ }
}

Avoid copying and pasting as it won’t help you remember or question why the code is written the way it has been written.

Let’s create our JSON object. You can create that within the test method body like so;

I don’t know if you noticed but I’ve used the gson dependency which provides its ownJsonObject class to create our json data.

Now that we have created a json object, we can pass in our class plus the json object and ask gson.fromJson() to do all the complicated mappings like so;

And now we can add our assertions to confirm that the object instantiated has been instantiated with the correct values.

Now if you run your test you should hopefully see it all go green! This is the overall snapshot of the test file;

As you can see its very simple to create a POJO from a json response or object.

JSON to Annotated POJO example

Now what if you had a class which was defined with specific fields in mind and you were dealing with a json object that had its own naming convention for those fields that you were interested in.

For example what if you had a json response with the following key/value:

json response

{
"custom_field_102": "Lee"
}

field name

private String name;

Gson provides an easy solution. You can add annotations to your fields with specific serialized name i.e. "custom_field_102" . This will be mapped across to your instance variables within your model. Here is the class that I created with the addition of the annotations;

Ensure that you have the relevant import in your class.

Just like the previous example I have created a test class and made some assertions to confirm that the annotations are working;

Can you see the difference between this test class and the previous test class.

The json object I have created has its own naming convention but my model has been predefined with fields that are applicable to my needs.

However, because we used annotations when defining our model, gson was able to find the correct fields from the json object and create our new AnnotationPojo object.

I hope you have found this blog useful. Please do leave me comments or give me feedback on mistakes that you notice.

You can find the full working code in the following repository;

Happy coding!

--

--