Apart from the trick solution using reflection, you could also try jackson with one line as follows:

objectMapper.convertValue(o, Map.class);

A test case:

    @Test
    public void testConversion() {
        User user = new User();
        System.out.println(MapHelper.convertObject(user));
    }

    @Data
    static class User {
        String name = "Jack";
        boolean male = true;
    }


// output: you can have the right type normally
// {name=Jack, male=true}
Answer from Hearen on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java collections › java map › converting object to map in java
Converting Object To Map in Java | Baeldung
January 8, 2024 - Furthermore, the resulting map has the employee object’s field names as keys and their corresponding values. Here’s another example showcasing the Jackson library to transform a Java object (employee) into a map representation while dealing with nested objects like Address within Employee:
🌐
Medium
medium.com › code-science › convert-java-object-to-map-edfb9080834d
Convert Java Object to Map. Scenario | by Ishaan Gupta | Code Science | Medium
May 5, 2023 - import java.util.Arrays; import java.util.Map; import com.fasterxml.jackson.databind.ObjectMapper; public class FooBar { public static convertObjectToMap(Employee employee) { ObjectMapper mapper = new ObjectMapper(); Map<String, Object> map= mapper.convertValue(employee, Employee.class); return map; } }
🌐
Mkyong
mkyong.com › home › java › java – convert object to map example
Java – Convert Object to Map example | mkyong.com
October 29, 2016 - package com.mkyong.examples; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Arrays; import java.util.Map; public class ObjectToMapExample { public static void main(String[] args) { ObjectMapper oMapper = new ObjectMapper(); Student obj = new Student(); obj.setName("mkyong"); obj.setAge(34); obj.setSkills(Arrays.asList("java","node")); // object -> Map Map<String, Object> map = oMapper.convertValue(obj, Map.class); System.out.println(map); } }
🌐
Baeldung
baeldung.com › home › json › jackson › intro to the jackson objectmapper
Intro to the Jackson ObjectMapper | Baeldung
December 9, 2025 - String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; Map<String, Object> map = objectMapper.readValue(json, new TypeReference<Map<String,Object>>(){}); One of the greatest strengths of the Jackson library is the highly customizable serialization and deserialization process. In this section, we’ll go through some advanced features where the input or the output JSON response can be different from the object that generates or consumes the response. While converting JSON objects to Java classes, in case the JSON string has some new fields, the default process will result in an exception:
🌐
Baeldung
baeldung.com › home › java › java collections › java map › converting jsonnode object to map
Converting JsonNode Object to Map | Baeldung
January 8, 2024 - Nested objects within the JSON structure are accessed by casting the corresponding values to Map<String, Object>, while lists are accessed by casting them to List<String>. The final assertion verifies the value associated with the isActive key.
🌐
How to do in Java
howtodoinjava.com › home › java basics › convert an object to map in java
Convert an Object to Map in Java
September 19, 2022 - Learn to convert a given Java object into a Map using different solutions including Jackson ObjectMapper, Gson and Java reflection.
🌐
Cassiomolin
cassiomolin.com › home › archive › converting pojo to map and vice versa with jackson
Converting POJO to Map and vice versa with Jackson · Cássio Molin
June 19, 2026 - // Create ObjectMapper instance ... mapper.convertValue(map, Foo.class); Copy · According to the Jackson documentation, the convertValue() method is functionally similar to first serializing given value into JSON, and then binding ...
Find elsewhere
🌐
Medium
medium.com › @aroranayan › java-convert-object-to-map-example-3244bf8b153e
Java — Convert Object to Map example | by Nayan Arora | Medium
May 30, 2019 - package com.nayan.examples;import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Arrays; import java.util.Map;public class ObjectToMapExample { public static void main(String[] args) { ObjectMapper oMapper = new ObjectMapper(); Student obj = new Student(); obj.setName("nayan"); obj.setAge(34); obj.setSkills(Arrays.asList("java","angular")); // object -> Map Map<String, Object> map = oMapper.convertValue(obj, Map.class); System.out.println(map); }}
🌐
Baeldung
baeldung.com › home › json › jackson › map serialization and deserialization with jackson
Map Serialization and Deserialization with Jackson | Baeldung
January 8, 2024 - Here we’re telling Jackson to deserialize the Map<MyPair, String> contained in ClassWithAMap, so we need to extend KeyDeserializer to describe how to deserialize the map’s key, a MyPair object, from an input String:
🌐
Stack Abuse
stackabuse.com › convert-json-string-to-java-map-with-jackson
Convert JSON String to Java Map with Jackson
March 16, 2023 - String json = "{\n" + "\"Task 1\" : \"In_Progress\",\n" + "\"Task 2\" : \"Done\",\n" + "\"Task 3\" : \"Planned\"\n" + "}"; // ObjectMapper instantiation ObjectMapper objectMapper = new ObjectMapper(); // Deserialization into a Map Map<String, TASK_STATUS> result = objectMapper.readValue(json, HashMap.class); // Printing the results System.out.println(result.entrySet()); We've chucked the json contents into the readValue() method, and since it contains JSON that can be deserialized into a Map, given the key-value pairings, told Jackson to deserialize into a HashMap.
🌐
Educative
educative.io › answers › how-to-convert-a-json-string-to-a-map-using-jackson
How to convert a JSON string to a map using Jackson
Class<T> valueType: the class to which the string has to be deserialised to. import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Map; public class Main { public static void main(String[] args) throws JsonProcessingException { String s = "{\"name\": \"educative\",\"website\": \"https://www.educative.io/\",\"description\": \"e-learning website\"}"; ObjectMapper objectMapper = new ObjectMapper(); System.out.println(objectMapper.readValue(s, Map.class)); } } Relevant Answers ·
🌐
HowToDoInJava
howtodoinjava.com › home › jackson › jackson – marshal and unmarshal java maps
Jackson - Marshal and Unmarshal Java Maps - HowToDoInJava
August 20, 2022 - Java program to convert JSON to Map is as follows. we are using TypeReference which is used to describe the type of our destination class type. String json = "{\"id\":\"1\",\"name\":\"Lokesh Gupta\",\"age\":34,\"location\":\"India\"}"; ObjectMapper mapper = new ObjectMapper(); HashMap<String, String> map = mapper.readValue(json, new TypeReference<Map<String, String>>(){});
🌐
Mkyong
mkyong.com › home › java › convert json string to map using jackson
Convert JSON string to Map using Jackson | mkyong.com
April 23, 2024 - package com.mkyong.json.jackson; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Map; public class JsonToMapExample { public static void main(String[] args) throws JsonProcessingException { // JSON string String json = "{\"name\": \"mkyong\", \"age\": 20}"; // Create an ObjectMapper instance. ObjectMapper mapper = new ObjectMapper(); // convert JSON string to Map // uncheck assignment // Map<String, Object> map = mapper.readValue(json, Map.class); // Convert JSON string to Map Map<String, Object> map = mapper.readValue(json, new TypeReference<>() {}); // Output the contents of the Map to verify the conversion System.out.println("Map content: " + map); } } Output ·
🌐
Jenkov
jenkov.com › tutorials › java-json › jackson-objectmapper.html
Jackson ObjectMapper
By default Jackson maps the fields of a JSON object to fields in a Java object by matching the names of the JSON field to the getter and setter methods in the Java object.
🌐
Thetechnojournals
thetechnojournals.com › 2019 › 10 › how-to-convert-java-object-to-map-or.html
How to convert java object to Map or Properties
October 19, 2019 - We will use this class to convert ... = name; this.age = age; } //getter methods //setter methods } Jackson library provides ObjectMapper class which provides "convertValue" method to convert the java objects....
🌐
Baeldung
baeldung.com › home › json › jackson › mapping a dynamic json object with jackson
Mapping a Dynamic JSON Object with Jackson | Baeldung
January 8, 2024 - We can map the common properties with the following Java class: class Product { String name; String category; // standard getters and setters } On top of that, we need an appropriate representation for the details object.
Top answer
1 of 2
5

That's depends what the input is.

  • (Map<String, Object>) o is used for casting conversion, so the runtime type of o must be Map, otherwise ClassCastException will be thrown:

    Object o = new HashMap<>();
    Map<String, Object> map = (Map<String, Object>) o; // ok
    
    Object o = new ArrayList<>();
    Map<String, Object> map = (Map<String, Object>) o; //ClassCastException
    
    Object o = new String();
    Map<String, Object> map = (Map<String, Object>) o; //ClassCastException
    
  • ObjectMapper().readValue(String content, JavaType valueType):

    Method to deserialize JSON content from given JSON content String.

    which means the input should be a String in valid json format. For example:

    Object input = "{\"key1\":{},\"key2\":{}}";
    Map<String, Object> map = new ObjectMapper().readValue((String) input, new TypeReference<Map<String, Object>>() {});
    
    System.out.println(map.size()); // 2
    
2 of 2
3

There is no "better". There are practical differences. But first, both options use type casting, and this raises the question "how are these two considered alternatives at all?"

Casting will only work if some object passes the instanceof Map test (first option), or if o passes the instanceof String test (second option).

In other words, the answer depends on the runtime class of some object.

So either this will fail with a class cast exception:

(Map<String, Object>) o;

Or this will fail with the same exception:

(String) o 

Assuming the type checks will pass one way or the other, there are differences to be considered:

  • If some object is compatible with Map<String, Object>, then casting should be preferred as it offers a performance advantage (considering objectMapper.convertValue as the alternative). In fact, readValue isn't even an option as the cast to string will fail.
  • If some object is a String, then you have no choice but to call readValue, as you need to parse JSON.

As noted above, there's also objectMapper.convertValue, which in a smart way checks first instanceof before performing the conversion through JSON parsing. It's meant to convert objects from one type to another, if the two types have the same fields.

🌐
Spring Framework Guru
springframework.guru › home › how to the jackson object mapper with json
How to The Jackson Object Mapper with JSON - Spring Framework Guru
October 22, 2024 - Adding the jackson-databind dependency to your project adds two transitive dependencies: ... Serialization is the process of converting a Java object into JSON. Remember, in Java everything is an object. Thus, Jackson’s object mapper can convert Java Maps, Lists, Java Types, Java Records, ...