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.

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 object

Before 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 dependency

You can add Jackson dependency in two ways depending on your project type.

1.1. Maven based project

Add following dependency in your pom.xml file.

com.fasterxml.jackson.core jackson-core 2.9.6

1.2. For ANT or other project types

For non-Maven use cases, you download jars from Central Maven repository.

2. Jackson ObjectMapper

ObjectMapper 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 object

Use below sample syntax to read JSON and populate java objects.

ObjectMapper mapper = new ObjectMapper(); Object value = mapper.readValue(jsonSource , javaObject);
  • jsonSource The input source which will fetch the json string.
  • javaObject The target Java object which needs to be populated.

2.2. Syntax to convert object to json

Use below sample syntax to write java object to json string.

ObjectMapper mapper = new ObjectMapper(); Object value = mapper.writeValue(jsonTarget, javaObject);
  • jsonTarget The output target where json string will be written.
  • javaObject The source Java object which needs to be converted to json.

3. Jackson convert object to JSON

To 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 output

If 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:

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 JavaToPrettyJSONExample { 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.defaultPrettyPrintingWriter().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" }

5. Jackson convert JSON to Java object

To 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