Java – Convert Object to Map

This tutorial provides several ways of converting a Java object to a Map in Java.

1. Reflection

The traditional way of converting a Java object to a Map is through using the reflection mechanism provided by the JDK.

Suppose we have a class called Student that holds 2 fields id and name. The following method converts the Student object to a Map<String, Object> using reflection:

2. Jackson

The other way of doing the conversion is through using Jackson library.

The following example uses the same example above for converting a Student object to Map<String, Object> using Jackson library.

3.5 4 votes
Article Rating

Hussein Terek

Founder of, I have a passion in software engineering and everything related to java environment.

You may also like...

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
4 months ago

Exception in thread “main” java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/exc/InputCoercionException
 at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.createMapDeserializer(
 at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(
 at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(
 at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(
 at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(
 at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(
 at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(
 at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(
 at com.fasterxml.jackson.databind.ObjectMapper._convert(
 at com.fasterxml.jackson.databind.ObjectMapper.convertValue(
 at com.test.json.ConvObjectToMap.main(
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.exc.InputCoercionException
 at java.lang.ClassLoader.loadClass(
 at sun.misc.Launcher$AppClassLoader.loadClass(
 at java.lang.ClassLoader.loadClass(
 … 11 more
Process finished with exit code 1