Convert Java Object, Array and List to Json String with Jackson

In the first article of our Jackson tutorial series, we learned how to convert JSON string into Java objects. In this article, we will explore the reverse process – converting Java objects, arrays, and lists into JSON strings using the ObjectMapper class.

For our examples, we’ll continue to use the Person class:

public class Person {
    private Long id;
    private String name;
    private String dateOfBirth;
    private List<Person> friends;
    // Getters and setters (not shown for brevity)
}

Java Object to JSON String

To convert a Person object into a JSON string, we’ll use the ObjectMapper class’s writeValueAsString method:

    private static void objectToString() throws JsonProcessingException {
        Person alice = new Person();
        alice.setId(1L);
        alice.setName("Alice");
        alice.setDateOfBirth("1990-05-15");

        Person bob = new Person();
        bob.setId(2L);
        bob.setName("Bob");
        bob.setDateOfBirth("1985-08-20");

        alice.setFriends(Arrays.asList(bob));

        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(alice);
        System.out.println(jsonString);
    }

The output:

{"id":1,"name":"Alice","dateOfBirth":"1990-05-15","friends":[{"id":2,"name":"Bob","dateOfBirth":"1985-08-20","friends":null}]}

Java Array to JSON String

If you have an array of Person objects and want to convert it to a JSON array:

    private static void arrayToString() throws JsonProcessingException {
        Person[] people = new Person[2];

        Person alice = new Person();
        alice.setId(1L);
        alice.setName("Alice");
        alice.setDateOfBirth("1990-05-15");

        Person bob = new Person();
        bob.setId(2L);
        bob.setName("Bob");
        bob.setDateOfBirth("1985-08-20");

        alice.setFriends(Arrays.asList(bob));

        people[0] = alice;
        people[1] = bob;

        ObjectMapper objectMapper = new ObjectMapper();
        String jsonArray = objectMapper.writeValueAsString(people);
        System.out.println(jsonArray);
    

The output:

 [{"id":1,"name":"Alice","dateOfBirth":"1990-05-15","friends":[{"id":2,"name":"Bob","dateOfBirth":"1985-08-20","friends":null}]},{"id":2,"name":"Bob","dateOfBirth":"1985-08-20","friends":null}]

Java List to JSON String

When dealing with a list of Person objects:

    private static void listToString() throws JsonProcessingException {
        List<Person> peopleList = new ArrayList<>();

        Person alice = new Person();
        alice.setId(1L);
        alice.setName("Alice");
        alice.setDateOfBirth("1990-05-15");

        Person bob = new Person();
        bob.setId(2L);
        bob.setName("Bob");
        bob.setDateOfBirth("1985-08-20");

        alice.setFriends(Arrays.asList(bob));

        peopleList.add(alice);
        peopleList.add(bob);

        ObjectMapper objectMapper = new ObjectMapper();
        String jsonArray = objectMapper.writeValueAsString(peopleList);
        System.out.println(jsonArray);
    }

The output:

[{"id":1,"name":"Alice","dateOfBirth":"1990-05-15","friends":[{"id":2,"name":"Bob","dateOfBirth":"1985-08-20","friends":null}]},{"id":2,"name":"Bob","dateOfBirth":"1985-08-20","friends":null}]

Read More