Java Jackson ObjectMapper Class with Examples


Post Banner

The ObjectMapper class from the com.fasterxml.jackson.databind is the class that can be used to convert Java Objects (mostly POJOs) to JSON and vice-versa.

To make use of the ObjectMapper, you will need to add the jackson-databind dependency in your Gradle/Maven Project,

How to add Jackson Dependecy in Gralde Project

How to add Jackson Dependecy in Maven Project



Example: ObjectMapper to convert JSON String to Java Object

Employee.java
public class Employee {

    String empName;
    int empAge;
    String empDept;

    public  Employee() {}
    
    public Employee(String empName, int empAge, String empDept) {
        this.empName = empName;
        this.empAge = empAge;
        this.empDept = empDept;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public int getEmpAge() {
        return empAge;
    }

    public void setEmpAge(int empAge) {
        this.empAge = empAge;
    }

    public String getEmpDept() {
        return empDept;
    }

    public void setEmpDept(String empDept) {
        this.empDept = empDept;
    }
}
import com.fasterxml.jackson.databind.ObjectMapper;

public class ObjectMapperExample {

    public static void main(String[] args) {

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            String employeeJson = "{ \"empName\" : \"Jake\", \"empAge\" : 35, \"empDept\" : \"Insurance\"}";

            Employee employee = objectMapper.readValue(employeeJson, Employee.class);
            System.out.println("Employee Name:" + employee.getEmpName());
            System.out.println("Employee Department:" + employee.getEmpDept());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Example: ObjectMapper to convert Java Object to Json String

    public static void main(String[] args) {

        try {
            Employee employee = new Employee("Jake",45,"Finance");
            ObjectMapper objectMapper = new ObjectMapper();
            String employeeJsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee);
            System.out.println("Employee Json :" + employeeJsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Output:
Employee Json :{
  "empName" : "Jake",
  "empAge" : 45,
  "empDept" : "Finance"
}


Have Questions? Post them here!
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.