Convert Java Object To / From JSON, Jackson Example
Convert Java Object To / From JSON, Jackson Example explains about converting a Java Object to JSON string, JSON string to Java Object Using Jackson JSON library
Consider an example where you need to pass the request as JSON string, In this situation you can easily serialize the Java Object to JSON string and vice versa
According to Jackson documentation, Jackson is a multi-purpose Java library for processing JSON data format. Jackson aims to be the best possible combination of fast, correct, lightweight, and ergonomic for developers.
Reference -> https://github.com/FasterXML/jackson/
Here I am showing an example about How to convert a Java Object to JSON string and vice versa using Jackson processor.
Required Libraries
Project structure
Student.java
Here I am creating a pojo class, this will be the converted in to JSON string.
public class Student {
private String firstName="Rockey";
private String lastName="Desousa";
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;
}
@Override
public String toString() {
return "Student [firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
Convert Java Student Object to JSON string
For converting Java Student Object to JSON string, you can use following method, Here student.json is a file
mapper.writeValue(new File("student.json"), student);
Convert JSON string to Java Student Object
For converting JSON string to Java Student Object, you can use following method
student = mapper.readValue(new File("student.json"), Student.class);
A complete example is below
Jackson JSON Example
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class Main {
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
Student student = new Student();
// stored into student.json file
mapper.writeValue(new File("student.json"), student);
student = mapper.readValue(new File("student.json"), Student.class);
// disply in console
System.out.println("student details" + student);
}
}
Output (student.json)
{"firstName":"Rockey","lastName":"Desousa"}
Console output
student details Student [firstName=Rockey, lastName=Desousa]
Jackson JSON Pretty Print
If you need to formatt the JSON string, you can use the following method
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
A complete example is below
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
public class Main {
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
Student student = new Student();
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String prettyJsonStudent = ow.writeValueAsString(student);
System.out.println(prettyJsonStudent);
}
}
Output
{ "firstName" : "Rockey", "lastName" : "Desousa" }