[Update Sept 2020] Although my original answer here, from many years ago, seems to be helpful and is still getting upvotes, I now use the GSON library from Google, which I find to be more intuitive.

I've got the following code:

public void testJackson() throws IOException {  
    ObjectMapper mapper = new ObjectMapper(); 
    File from = new File("albumnList.txt"); 
    TypeReference<HashMap<String,Object>> typeRef 
            = new TypeReference<HashMap<String,Object>>() {};

    HashMap<String,Object> o = mapper.readValue(from, typeRef); 
    System.out.println("Got " + o); 
}   

It's reading from a file, but mapper.readValue() will also accept an InputStream and you can obtain an InputStream from a string by using the following:

new ByteArrayInputStream(astring.getBytes("UTF-8")); 

There's a bit more explanation about the mapper on my blog.

Answer from djna 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 - The following example shows how we can convert a Java object (employee) into a map using Jackson: @Test public void givenJavaObject_whenUsingJackson_thenConvertToMap() { ObjectMapper objectMapper = new ObjectMapper(); Map<String, Object> map = objectMapper .convertValue(employee, new TypeReference<Map<String, Object>>() {}); Assert.assertEquals(employee.getName(), map.get("name")); Assert.assertEquals(employee.getAge(), map.get("salary")); }
Discussions

java - How to map an object inside a string to set using ObjectMapper - Stack Overflow
Consider using Deserialization... in your ObjectMapper instance. Then you must change the json field countryCode to countries or mark Set countries as json property countryCode using @JsonProperty and provide a constructor/factory method within String parameter. This will make your object mapper behave as ... More on stackoverflow.com
🌐 stackoverflow.com
March 30, 2017
String with JSON inside to HashMap (instead to objects)
Your JSON is not an object but an array. You'll have to deserialise it to a list of Maps, not a Map. More on reddit.com
🌐 r/javahelp
6
1
March 17, 2020
Object Mapper (Swift 4) - Array of objects in parent object doesn’t load/partially fills data

Two things,

  1. I’ve never used mapping(map: map) within the init. That shouldn’t be necessary. Keep the body of the init method empty. Only return nil there if your JSON is invalid.

  2. You should go back to using routineTasks <- map["_routineTasks"] instead of routineTasks = [RoutineTask(map: map)]. Make sure that key "_routineTasks" is correct.

Also, try setting a breakpoint in the mapping method and doing a po map.JSON to confirm what you’re expecting is actually there.

More on reddit.com
🌐 r/iOSProgramming
28
5
January 17, 2018
How to convert json string to pojo if json response starts with [] instead of {} ??
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnjava
12
1
August 21, 2023
🌐
Baeldung
baeldung.com › home › json › jackson › map serialization and deserialization with jackson
Map Serialization and Deserialization with Jackson | Baeldung
January 8, 2024 - It allows us to serialize our map, and write it out as a pretty-printed JSON String using the toString() method in String: ... With a few extra steps, we can also serialize a map containing a custom Java class.
🌐
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 - public static convertObjectToMap(T object) { ObjectMapper mapper = new ObjectMapper(); Map<String, Object> map= mapper.convertValue(object, T.class); return map; } Now you can reuse this function to convert any object into a map. Stack Overflow · Java · Objects ·
🌐
GitHub
github.com › FasterXML › jackson-databind › discussions › 4330
handling Map<String,Object> of known types but not polymorphic · FasterXML/jackson-databind · Discussion #4330
But this approach has an effect on any generic object, and so other instances of Map<String,Object> fail to deserialise. It works if I add type info into the JSON as prescribed, but I can't alter some clients who use these interfaces.... (Note: it's mostly things like configurations where most of the time its a primitive, but occasionally some structured data that will evolve slowly) I could use a different customised ObjectMapper in a few instances if necessary.
🌐
Attacomsian
attacomsian.com › blog › jackson-convert-json-string-to-map
Convert JSON String to Map using Jackson
October 14, 2022 - Map<String, Object> map = new ObjectMapper().readValue( Paths.get("user.json").toFile(), Map.class );
🌐
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. ... But it uses a raw Map. This is not the right approach - not type-safe and not self-documenting. Or, I can use Jackson’s TypeReference with my ObjectMapper: ... This new TypeReference<Map<String, Object>>() {} means the map is no longer untyped.
🌐
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
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.0-rc1</version> </dependency> Create an object of the ObjectMapper class.
Find elsewhere
🌐
Stack Abuse
stackabuse.com › convert-json-string-to-java-map-with-jackson
Convert JSON String to Java Map with Jackson
March 16, 2023 - Much like you'd map JSON values to other types, to convert JSON contents into a Java Map, you'll use the readValue() method of the ObjectMapper instance, which deserializes it into the provided class reference: 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());
🌐
Mkyong
mkyong.com › home › java › convert json string to map using jackson
Convert JSON string to Map using Jackson | mkyong.com
April 23, 2024 - String json = "..."; ObjectMapper mapper = new ObjectMapper(); // convert JSON string to Map Map<String, Object> map = mapper.readValue(json, new TypeReference<>() {});
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Casting Maps into Complex Objects in Java - Java Code Geeks
June 21, 2024 - It can be used to convert a Map to a POJO using its ObjectMapper class. ... <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.17.1</version> </dependency> ... public class ...
🌐
javathinking
javathinking.com › blog › convert-string-to-map-in-java-using-objectmapper
Convert String to Map in Java Using ObjectMapper — javathinking.com
Jackson is a powerful and flexible library for working with JSON data in Java. The ObjectMapper class provides a straightforward and convenient way to perform serialization and deserialization between Java objects and JSON strings.
🌐
Makeinjava
makeinjava.com › home › convert map to/from json string in java (jackson objectmapper/ example)
Convert Map to/from JSON string in java (jackson objectmapper example)
January 1, 2024 - JSON to Map //Define Custom Type reference for map type TypeReference<Map<String, Person>> mapType = new TypeReference<Map<String,Person>>() {}; Map<String,Person> jsonToMap = objectMapper.readValue(mapToJson, mapType); System.out.println("\n2.
🌐
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 - Employee employee = new Employee(1, "Alex", LocalDate.of(1995, 1, 2), List.of("Delhi", "Nevada"), List.of(new Role(11, "Finance"), new Role(12, "HR"))); System.out.println(convertObjectToMapUsingObjectMapper(employee)); //The conversion method static Map<String, String> convertObjectToMapUsingObjectMapper(Employee employee) { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JavaTimeModule()); objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); return objectMapper.convertValue(employee, Map.class); }
🌐
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 ObjectMapper mapper = new ObjectMapper(); // Converting POJO to Map Map<String, Object> map = mapper.convertValue(foo, new TypeReference<Map<String, Object>>() {}); // Convert Map to POJO Foo anotherFoo = ...
🌐
Mkyong
mkyong.com › home › java › java – convert object to map example
Java – Convert Object to Map example | mkyong.com
October 29, 2016 - oMapper.convertValue(obj, new TypeReference<Map<String, Object>>() {});
🌐
Initial Commit
initialcommit.com › blog › java-convert-object-to-map
Java – Convert Object to Map - Initial Commit
October 25, 2019 - The following example uses the same example above for converting a Student object to Map<String, Object> using Jackson library. private void convertObjToMapJackson() { ObjectMapper oMapper = new ObjectMapper(); Student student = new Student(); student.setId(1); student.setName("Terek"); Map<String, Object> studentMap = oMapper.convertValue(student, Map.class); System.out.println(studentMap); }
🌐
Java Guides
javaguides.net › 2023 › 10 › convert-json-string-to-map-in-java.html
Convert JSON string to Map in Java
October 26, 2023 - import com.fasterxml.jackson.d... Smith\",\"email\":\"jane.smith@example.com\"}"; // Create an ObjectMapper instance ObjectMapper mapper = new ObjectMapper(); try { // Convert JSON string to Map Map<String, Object> map = mapper.read...
🌐
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 - In the example above we create a Map with a key using a String, and values of any object. The first two properties are simple Strings, while the address is a nested Map. In the last two lines, you can see we create a new ObjectMapper instance, and use it to serialize (writeValueAsString) the Map value to a JSON string, which is outputted to the console.