Convert JSON String to POJO Class Object in Java
In this tutorial, we will show you two different methods to convert JSON String to a Java POJO Class object:
Using ObjectMapper
ObjectMapper provides methods for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionality for performing conversions.
To use ObjectMapper class, you need to install jackson-databind dependency to your project.
In a Spring Boot project, the jackson-databind dependency is by default included in spring-boot-starter-web starter library. The Jackson version included that comes with Spring Boot starter library allows you to write read/write JSON data. However, it is a good practice to specify this dependency because Spring update could change the functionality.
Let the JSON string that we need to convert to a Java Class object be as follows:
"{
\"id\": 1,
\"firstName\": \"Danny\",
\"lastName\": \"Brown\",
\"active\": true
}"
To convert the above data into a Java Class object, do the following:
- Create a Java POJO class with fields whose names exactly match to the JSON key names:
- Create an object of ObjectMapper class and use the readValue() method to convert JSON String data into a Java class object as shown in the example below:
public class User {
private int id;
private String firstName;
private String lastName;
private boolean active;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Example {
public static void main(String[] args) {
// example json string data
String jsonStringExampleData = "{\"id\":1,\"firstName\":\"Danny\", \"lastName\":\"Brown\", \"active\":true}";
// creating an object of ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
try {
// converting from JSON string to Class object
User user = objectMapper.readValue(jsonStringExampleData, User.class);
// printing output
System.out.println("Id = " + user.getId());
System.out.println("First name = " + user.getFirstName());
System.out.println("Last name = " + user.getLastName());
System.out.println("Active = " + user.isActive());
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Using Gson Library
- Add Gson dependency to your project.
- Next, you can create an object of Gson and use fromJson() method to convert JSON String to POJO Class object as shown in the example below:
import com.google.gson.Gson;
public class Example {
public static void main(String[] args) {
// example json string data
String jsonStringExampleData = "{\"id\":1,\"firstName\":\"Danny\", \"lastName\":\"Brown\", \"active\":true}";
// creating an object of Gson
Gson gson = new Gson();
// converting from json string to User class object
User user = gson.fromJson(jsonStringExampleData, User.class);
// printing output
System.out.println("Id = " + user.getId());
System.out.println("First name = " + user.getFirstName());
System.out.println("Last name = " + user.getLastName());
System.out.println("Active = " + user.isActive());
}
}