Learn how to convert String to JSON Object in Java using Jackson ObjectMapper with a simple example and readValue() method. Jackson provides the ObjectMapper class, which can deserialize a JSON string into a corresponding Java object.
Code Explanation (Step-by-Step)
- Here, we create two classes: StudentDepartment, and ConvertStringToJson.
- The StudentDepartment class contains two fields: branch and name. The @JsonProperty annotation is used to map JSON properties to these fields.
- The stringToJsonData() method takes two parameters: a JSON string and the target class type, such as StudentDepartment.class.
- Inside the stringToJsonData() method, we create an ObjectMapper object.
- The mapper.readValue(string, clazz) method deserializes the JSON string into an object of the specified Java class.
- Now the method returns the converted Java object.
Jackson Maven Dependency :-
com.fasterxml.jackson.core
jackson-databind
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
class StudentDepartment {
private String branch;
private String name;
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "StudentDepartment{" +
"branch='" + branch + '\'' +
", name='" + name + '\'' +
'}';
}
}
public class ConvertStringToJson {
public static void main(String[] args) {
String jsonString =
"{\"branch\":\"Computer Science\",\"name\":\"Santosh\"}";
try {
StudentDepartment studentDepartment =
stringToJsonData(
jsonString,
StudentDepartment.class
);
System.out.println(studentDepartment);
} catch (IOException e) {
e.printStackTrace();
}
}
private static T stringToJsonData(
String jsonString,
Class clazz) throws IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(jsonString, clazz);
}
}
Output :-
StudentDepartment{branch=’Computer Science’, name=’Santosh’}
Handle Invalid JSON Strings in Java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class InvalidJsonExample {
public static void main(String[] args) {
// Invalid JSON String
String jsonString = "{\"id\":101,\"name\":\"Santosh\"";
ObjectMapper mapper = new ObjectMapper();
try {
Student student = mapper.readValue(jsonString, Student.class);
System.out.println("Student ID: " + student.getId());
System.out.println("Student Name: " + student.getName());
} catch (JsonProcessingException e) {
System.out.println("Invalid JSON String");
System.out.println("Error: " + e.getMessage());
}
}
}
``
Output :-
Invalid JSON String
Error: …
Is @JsonProperty required here?
No. Because your JSON property names and Java field names are already identical, Jackson can map them without @JsonProperty.
It becomes particularly useful when the names are different. For example:
@JsonProperty("student_name")
private String name;
{
"student_name": "Santosh"
}