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....
🌐
Northcoder
northcoder.com › post › jackson-object-mapper-which-way-is
Jackson's ObjectMapper and TypeReference | northCoder
January 15, 2021 - Some notes on Jackson’s ObjectMapper, when using it without any custom POJO classes, to deserialize an arbitrary piece of JSON to a Java Map.
Top answer
1 of 2
3

You can use Jackson ObjectMapper with TypeReference , First you need to read it as Map<String,Object> then you can extract the name and num with Java.

you need to get the data from obj then for each item in the dataList you have to map it to a new HashMap as shown below

ObjectMapper objectMapper = new ObjectMapper ();
Map<String,Object> jsonMap = objectMapper.readValue(jsonData,new TypeReference<Map<String,Object>>(){});
List<Object> data = ((Map<String,Object>)jsonMap.get("obj")).get("data");
List<Map<String,String> result = data.stream()
   .map(d->(Map<String,Object> d)
   .map(d->{
      Map<String,String> map = new HashMap();
      map.put(d.get("name"),((Map<String,String>)d.get("value")).get("num"));
      return map;
      })
   .collect(Collectors.toList());

However if you can create a class for the the data it will be bit easier.

2 of 2
1

Library Josson can do the job.

https://github.com/octomix/josson

Deserialization

Josson josson = Josson.fromJsonString(
    "{" +
    "  \"code\": 1," +
    "  \"msg\": \"query success\"," +
    "  \"obj\": {" +
    "    \"data\": [" +
    "      {" +
    "        \"name\": \"\"," +
    "        \"value\": {" +
    "          \"num\": \"89\"" +
    "        }" +
    "      }," +
    "      {" +
    "        \"name\": \"1\"," +
    "        \"value\": {" +
    "          \"num\": \"19\"" +
    "        }" +
    "      }," +
    "      {" +
    "        \"name\": \"2\"," +
    "        \"value\": {" +
    "          \"num\": \"6\"" +
    "        }" +
    "      }" +
    "    ]" +
    "  }," +
    "  \"success\": true," +
    "  \"duration\": 523" +
    "}");

Transformation

JsonNode node = josson.getNode("obj.data.map(name::value.num)");
System.out.println(node);

List<Map<String, String>> result = Josson.readValueFor(node, List.class);
System.out.println(result);

Output

[{"":"89"},{"1":"19"},{"2":"6"}]

[{=89}, {1=19}, {2=6}]
🌐
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.