How to Convert Object to Map in Java using Jackson

Published May 10, 2022

How can we convert an object into a map in Java?

We can easily use Jackson to achieve this.

Suppose we have a class Person.

Person person = new Person();
person.setName("Bob");
person.setAge(30);

We want to convert this object to a map, one that looks like this:

{name=Bob, age=30}

We can use ObjectMapper.convertValue() to convert this object to a map.

ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.convertValue(person, Map.class);