How to Convert Object to Map in Java using Jackson
How can we convert an object into a map in Java using Jackson?
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 perform the conversion from Person.class
to Map.class
.
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.convertValue(person, Map.class);