In this post we will see how to Convert Java Map to/from JSON using Jackson.For this we will be using the ObjectMapper.readValue and ObjectMapper.writeValue
We will have a map with employee object for the conversion
Employee.java
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 48 49 50 51 52 |
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; } @Override public String toString() { return "Employee [empId=" + empId + ", name=" + name + ", department=" + department + "]"; } } |
1. Converting Java Map 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 31 32 33 34 35 |
package com.kscodes.json.jackson; import java.io.File; import java.util.HashMap; import java.util.Map; import com.fasterxml.jackson.databind.ObjectMapper; public class JavaMapToJSONExample { public static void main(String[] args) { // Create a Map of Employee Object and put data Map<String, Employee> mapEmployee = new HashMap<>(); mapEmployee.put("001", new Employee(1, "John Doe", "Sales")); mapEmployee.put("002", new Employee(2, "Smith T", "Management")); mapEmployee.put("003", 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\\JavaMapToJson.json"); try { mapper.writeValue(jsonFile, mapEmployee); System.out.println("JSON file written !!!"); } catch (Exception e) { System.out.println("Exception occurred while writing JSON File"); System.out.println("Exception::" + e); } } } |
Output – JavaMapToJson.json
A file will be created with the below contents
1 2 3 4 5 |
{ "001":{"empId":1,"name":"John Doe","department":"Sales"}, "002":{"empId":2,"name":"Smith T","department":"Management"}, "003":{"empId":3,"name":"Scott Tiger","department":"HR"} } |
2. Converting JSON to Java Map
We will use the same file that was created in the above code to recreate the Map object.
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 |
package com.kscodes.json.jackson; import java.io.File; import java.util.Map; import com.fasterxml.jackson.databind.ObjectMapper; public class JSONToJavaMapExample { public static void main(String[] args) { ObjectMapper mapper = new ObjectMapper(); try { File lstJsonFile = new File("C:\\kscodes\\JavaMapToJson.json"); Map<String, Employee> mapEmployee = mapper.readValue(lstJsonFile, Map.class); System.out.println("Printing the Map after Conversion"); for (Map.Entry<String, Employee> entry : mapEmployee.entrySet()) { System.out.println(entry.getKey() + " :: " + entry.getValue()); } } catch (Exception e) { System.out.println("Exception occurred while reading JSON File"); System.out.println("Exception::" + e); } } } |
Output
1 2 3 4 |
Printing the Map after Conversion 001 :: {empId=1, name=John Doe, department=Sales} 002 :: {empId=2, name=Smith T, department=Management} 003 :: {empId=3, name=Scott Tiger, department=HR} |