Jackson is a easy to use API that can help in the Java to/from JSON transformation.
In this post lets see how to convert java object to json using Jackson.
Add Jackson dependency
Please note we are using jackson 2 in our examples
The latest Jackson version during writing this post was 2.8.3
1 2 3 4 5 |
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.3</version> </dependency> |
If you are not using Maven you will need to download 3 jars
1. Jackson Databind 2.8.3 jar
2. Jackson Core 2.8.3 jar
3. Jackson Annotations 2.8.3 jar
Employee.java
Now lets start the example. First we will create a Employee Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.kscodes.json.jackson; public class Employee { private int empId; private String name; private String department; public Employee() { super(); } public Employee(int empId, String name, String department) { super(); this.empId = empId; this.name = name; this.department = department; } public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } } |
JavaObjectToJSON.java
Now the Class that will convert Java to JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.kscodes.json.jackson; import java.io.File; import com.fasterxml.jackson.databind.ObjectMapper; public class JavaObjectToJSON { public static void main(String[] args) { // Create a Employee Object and add data Employee employee = new Employee(1, "John Doe", "Sales"); // Create an ObjectMapper object ObjectMapper mapper = new ObjectMapper(); // Create file object to write JSON into File jsonFile = new File("C:\\kscodes\\jacksonJSONTest.json"); try { mapper.writeValue(jsonFile, employee); System.out.println("JSON file written !!!"); } catch (Exception e) { System.out.println("Exception occurred while writeing JSON File"); System.out.println("Exception::" + e); } } } |
Output
A JSON file will be created with following data
1 |
{"empId":1,"name":"John Doe","department":"Sales"} |
Writing a Java List into JSON
Lets change the above code and add a List of Employee instead of a single employee. Then we will write this List into JSON and see the output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Create a List of Employee Object and add data List<Employee> listEmployee = new ArrayList<>(); listEmployee.add(new Employee(1, "John Doe", "Sales")); listEmployee.add(new Employee(2, "Smith T", "Management")); listEmployee.add(new Employee(3, "Scott Tiger", "HR")); // Create an ObjectMapper object ObjectMapper mapper = new ObjectMapper(); // Create file object to write JSON into File jsonFile = new File("C:\\kscodes\\jacksonJSONTest.json"); try { mapper.writeValue(jsonFile, listEmployee); System.out.println("JSON file written !!!"); } catch (Exception e) { System.out.println("Exception occurred while writeing JSON File"); System.out.println("Exception::" + e); } |
Output
1 2 3 4 5 |
[ {"empId":1,"name":"John Doe","department":"Sales"}, {"empId":2,"name":"Smith T","department":"Management"}, {"empId":3,"name":"Scott Tiger","department":"HR"} ] |