Convert List of objects to JSON spring boot
Jackson convert object to JSON example and convert json to object example. Learn to use jackson objectmapper to populate java object from json string and write json string from java object. Show Jackson is used to convert java object to json, and convert json to java object. In this quick jackson tutorial, I am giving examples of converting java objects to/from json programmatically. Table of Contents 1. Jackson maven dependency 2. Convert Java object to JSON 3. Pretty print JSON 4. Convert JSON to Java objectBefore jumping into code examples, lets define a simple pojo class which we will use in this example for conversion purpose. public class Employee { private Integer id; private String firstName; private String lastName; public Employee(){ } public Employee(Integer id, String firstName, String lastName, Date birthDate){ this.id = id; this.firstName = firstName; this.lastName = lastName; } //Getters and setters @Override public String toString() { return "Employee [id=" + id + ", firstName=" + firstName + ", " + "lastName=" + lastName + "]"; } }1. Jackson dependencyYou can add Jackson dependency in two ways depending on your project type. 1.1. Maven based projectAdd following dependency in your pom.xml file. 1.2. For ANT or other project typesFor non-Maven use cases, you download jars from Central Maven repository. 2. Jackson ObjectMapperObjectMapper is the main api used for data-binding. It comes with several reader/writer methods to preform the conversion from/to Java and JSON. It will use instances of JsonParser and JsonGenerator for implementing actual reading/writing of JSON. 2.1. Syntax to convert json to objectUse below sample syntax to read JSON and populate java objects. ObjectMapper mapper = new ObjectMapper(); Object value = mapper.readValue(jsonSource , javaObject);
2.2. Syntax to convert object to jsonUse below sample syntax to write java object to json string.
3. Jackson convert object to JSONTo convert the employee object and write it to some file, to can use below code. package test.jackson; import java.io.File; import java.io.IOException; import java.util.Date; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; public class JavaToJSONExample { public static void main(String[] args) { @SuppressWarnings("deprecation") Employee employee = new Employee(1, "Lokesh", "Gupta", new Date(1981,8,18)); ObjectMapper mapper = new ObjectMapper(); try { mapper.writeValue(new File("c://temp/employee.json"), employee); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }Program Output. {"id":1,"firstName":"Lokesh","lastName":"Gupta"}4. Jackson pretty print JSON outputIf you look at above output, then the output written in text file is very raw and not formatted. You can write a formatted JSON content using defaultPrettyPrintingWriter() writerWithDefaultPrettyPrinter instance like below: Program Output. { "id" : 1, "firstName" : "Lokesh", "lastName" : "Gupta" }5. Jackson convert JSON to Java objectTo convert a json string to java object (e.g. Employee object) use below code: package test.jackson; 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 JSONToJavaExample { public static void main(String[] args) { Employee employee = null; ObjectMapper mapper = new ObjectMapper(); try { employee = mapper.readValue(new File("c://temp/employee.json"), Employee.class); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(employee); } }Program Output. Employee [id=1, firstName=Lokesh, lastName=Gupta]Make sure you have defined a default constructor in your POJO class (e.g. Employee.java in our case). Jackson uses default constructor to create the instances of java class using reflection. If default constructor is not provided, then you will get JsonMappingException in runtime. Happy Learning !! Was this post helpful?Let us know if you liked the post. Thats the only way we can improve. Yes No |