Convert JSON to Java Object using Jackson
Jackson provides APIs which is a high-performance JSON processor for Java. Using Jackson APIs, you can easily convert a JSON to Java objects and vice-versa.
First you need to download following jars or using maven dependency
jackson-databind-2.9.8jackson-annotations-2.9.8
jackson-core-2.9.8
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8 </dependency>
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.8</version> </dependency>
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.8</version> </dependency>
Below is the JSON String that you want to convert to Java Object using Jackson
{ "id":12345, "name":"Pierce Brosnan", "age":35, "emailaddress":["p.brosnan@gmail.com", "p.brosnan@yahoo.com"] }
Java POJO class used to convert JSON String to object and vice-versa.
import java.util.List; public class Actor { private int id; private String name; private int age; private List<String> emailaddress; // removed getter and setter @Override public String toString() { return "Employee [id=" + id + ", name=" + name + "," +" age=" + age + ", emailaddress=" + emailaddress + "]"; } }
1) Convert JSON string to Java object using Jackson.
// Parses JSON String using Jackson Parser import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonToObject { public static void main(String[] args) { String jsonString = "{\"id\":12345, \"name\":\"Pierce Brosnan\", \"age\":35," + " \"emailaddress\":[\"p.brosnan@gmail.com\", \"p.brosnan@yahoo.com\"]}"; ObjectMapper mapper = new ObjectMapper(); Actor actor = null; try { actor = mapper.readValue(jsonString, Actor.class); } catch (IOException e) { e.printStackTrace(); } System.out.println(actor); } }
Output :
Actor [id=12345, name=Pierce Brosnan, age=35, emailaddress=[p.brosnan@gmail.com, p.brosnan@yahoo.com]]
2) Reads JSON file and converts it to Java object using Jakckson.
// Parses JSON File using Jackson Parser import java.io.File; import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonToObject { public static void main(String[] args) { File file = new File("c://actors.json"); // to read from file ObjectMapper mapper = new ObjectMapper(); Actor actor = null; try { actor = mapper.readValue(jsonString, Actor.class); } catch (IOException e) { e.printStackTrace(); } System.out.println(actor); } }
Output :
Actor [id=12345, name=Pierce Brosnan, age=35, emailaddress=[p.brosnan@gmail.com, p.brosnan@yahoo.com]]
3) Convert Java object to JSON string using Jackson.
// Converts Java object to JSON string using Jackson import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.databind.ObjectMapper; public class ObjectToJson { public static void main(String[] args) { Actor actor = new Actor(); actor.setId(1234); actor.setName("Pierce Brosnan"); actor.setAge(40); List<String> emailaddress = new ArrayList<>(); emailaddress.add("p.brosnan@yahoo.com"); emailaddress.add("p.brosnan@gmail.com"); actor.setEmailaddress(emailaddress); ObjectMapper mapper = new ObjectMapper(); String jsonString = ""; try { jsonString = mapper.writeValueAsString(actor); System.out.println(jsonString); // Enable pretty print jsonString = mapper.writerWithDefaultPrettyPrinter(). writeValueAsString(actor); System.out.println("Prettyfied output"); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } }
Output :
{"id":1234,"name":"Pierce Brosnan","age":40,"emailaddress":["p.brosnan@yahoo.com","p.brosnan@gmail.com"]} Prettyfied output { "id" : 1234, "name" : "Pierce Brosnan", "age" : 40, "emailaddress" : [ "p.brosnan@yahoo.com", "p.brosnan@gmail.com" ] }
4) Convert JSON to Map object
Map<String, String> map = new HashMap<>(); try { map = mapper.readValue(jsonString, Map.class); } catch (IOException e) { e.printStackTrace(); } System.out.println(map);
Output :
{id=12345, name=Pierce Brosnan, age=35, emailaddress=[p.brosnan@gmail.com, p.brosnan@yahoo.com]}
5) Convert Map object to JSON string
// Converts Map to JSON String import java.io.IOException; import java.util.Map; import java.util.HashMap; import com.fasterxml.jackson.databind.ObjectMapper; public class TestJsonJackson { public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("id", "12345"); map.put("name", "Pierce Brosnan"); map.put("age", "36"); try { jsonString = mapper.writeValueAsString(map); System.out.println(jsonString); // Pretty JSON jsonString = mapper.writerWithDefaultPrettyPrinter() .writeValueAsString(map); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } }
Output :
{ "name" : "Pierce Brosnan", "id" : "12345", "age" : "36" }
6) Below example shows how to convert JSON Array String to List object
Java class used to convert JSON Array String to List.
public class Employee { private int id; private String name; private int age; // removed getter and setter @Override public String toString() { return "Employee [id=" + id + ", name=" + name + "," +" age=" + age + "]"; } }
// Converts JSON Array String to List import java.io.IOException; import java.util.Arrays; import java.util.List; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; public class TestJsonJackson { public static void main(String[] args) { String jsonString = "[{\"id\":\"12345\", \"name\":\"Jason Bourne\", \"age\":\"35\"}," + "{\"id\":\"12346\", \"name\":\"James Bond\", \"age\":\"38\"}]"; ObjectMapper mapper = new ObjectMapper(); try { Employee[] employee = mapper.readValue(jsonString, Employee[].class); System.out.println(Arrays.toString(employee)); List<Employee> employeeList = Arrays.asList(mapper.readValue(jsonString, Employee[].class)); System.out.println(employeeList); employeeList = mapper.readValue(jsonString, new TypeReference<List<Employee>>() {}); System.out.println(employeeList); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Output :
[Employee [id=12345, name=Jason Bourne, age=35], Employee [id=12346, name=James Bond, age=38]] [Employee [id=12345, name=Jason Bourne, age=35], Employee [id=12346, name=James Bond, age=38]] [Employee [id=12345, name=Jason Bourne, age=35], Employee [id=12346, name=James Bond, age=38]]