Convert list of objects to/from JSON in java (jackson objectmapper/ example)

  • Given a list of user defined objects, we would like to convert list of pojo objects to JSON (and JSON to list of objects).
  • We will use the jackson’s objectmapper, to serialize list of objects to JSON & deserialize JSON to List of objects.
  • We will create Person class & we will perform following operations with Person class.
    • Convert List of Person objects to JSON
    • Convert the JSON to List of Person objects.
  • We have already discussed (similar transformations):
    • Convert Objects to/from JSON (jackson)
    • Convert map to/from JSON      (jackson)

1. Jackson objectMapper maven dependencies

<dependency>
 <groupId>com.fasterxml.jackson.core</groupId> 
 <artifactId>jackson-databind</artifactId>
 <version>2.7.1</version> 
</dependency>

2. Convert list of objects to/from JSON in java (jackson objectmapper)

2.1. Person Class:

  • Person class containing attributes is as follows:
package org.learn;

public class Person {
 public String firstName;
 public String lastName;
 public int age;
 public Person() {  
 }
 public Person(String firstName, String lastName,
    int age) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
 }
 public String toString() {
     return "[" + firstName + " " + lastName +
         " " + age +"]";
 }
}

2.2. JSONListConverter Class:

JSONListConverter  class is responsible for performing following operations.

  1. Convert List of Person objects to JSON String in java.
  2. Convert JSON String to List of Person objects in java.
package org.learn;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JSONListConverter 
{
    public static void main( String[] args ) throws IOException
    {
     ObjectMapper objectMapper = new ObjectMapper();
     //Set pretty printing of json
     objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

     //Define map which will be converted to JSON
     List<Person> personList = Stream.of(
    new Person("Mike", "harvey", 34),
      new Person("Nick", "young", 75),
    new Person("Jack", "slater", 21 ),
    new Person("gary", "hudson", 55))
    .collect(Collectors.toList());
     
     //1. Convert List of Person objects to JSON
     String arrayToJson = objectMapper.writeValueAsString(personList);
     System.out.println("1. Convert List of person objects to JSON :");
     System.out.println(arrayToJson);
     
     //2. Convert JSON to List of Person objects
     //Define Custom Type reference for List<Person> type
     TypeReference<List<Person>> mapType = new TypeReference<List<Person>>() {};
     List<Person> jsonToPersonList = objectMapper.readValue(arrayToJson, mapType);
     System.out.println("\n2. Convert JSON to List of person objects :");
     
     //Print list of person objects output using Java 8
     jsonToPersonList.forEach(System.out::println);
    }
}

Download Example Code – Jackson List Object to JSON

3. Output – list of objects to/from JSON in java (jackson objectmapper)

1. Convert List of person objects to JSON :
[ {
  "firstName" : "Mike",
  "lastName" : "harvey",
  "age" : 34
}, {
  "firstName" : "Nick",
  "lastName" : "young",
  "age" : 75
}, {
  "firstName" : "Jack",
  "lastName" : "slater",
  "age" : 21
}, {
  "firstName" : "gary",
  "lastName" : "hudson",
  "age" : 55
} ]

2. Convert JSON to List of person objects :
[Mike harvey 34]
[Nick young 75]
[Jack slater 21]
[gary hudson 55]
Scroll to Top