Quick revision
The short answer
We use ObjectMapper.convertValue: mapper.convertValue(map, Customer.class) turns the map into a Customer, and mapper.convertValue(customer, new TypeReference<Map<String, Object>>() {}) turns it back. convertValue serializes into an in-memory token buffer and deserializes from it, so our annotations and custom serializers apply without any JSON string. The catch is typing in the map: numbers become Integer, Long or Double and dates become strings or numbers, which can surprise consumers; treeToValue and valueToTree do the same job between JsonNode and POJOs.
What to remember
In this answer, we converted a Map<String, Object> into a Customer with mapper.convertValue(map, Customer.class) and back with a TypeReference<Map<String, Object>>. We saw that convertValue goes through a token buffer, so annotations and custom serializers apply, that maps hold Integer, Long or Double numbers and string or numeric dates that may surprise consumers, and that valueToTree and treeToValue do the same between JsonNode and POJOs.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We use ObjectMapper.convertValue: mapper.convertValue(map, Customer.class) turns the map into a Customer, and mapper.convertValue(customer, new TypeReference<Map<String, Object>>() {}) turns it back. convertValue serializes into an in-memory token buffer and deserializes from it, so our annotations and custom serializers apply without any JSON string. The catch is typing in the map: numbers become Integer, Long or Double and dates become strings or numbers, which can surprise consumers; treeToValue and valueToTree do the same job between JsonNode and POJOs.
Why this question is asked
Many messaging clients and frameworks hand us payloads as a Map<String, Object>, the generic shape of JSON in Java. Writing the map to a JSON string just to read it back as an object works, but it wastes CPU and memory and looks clumsy in review. Interviewers ask this coding question to see whether we know Jackson's conversion methods and, more importantly, their surprises. In this answer, we will write the conversion both ways, see how convertValue works inside, look at the number and date types that come out of a map, and finish with the tree model equivalents.
1A Quick Recap of the Two Shapes
A typed object such as Customer has fields with fixed Java types: a long id, a String name, a LocalDate for the date of birth. A Map<String, Object> is untyped: each value is whatever the producer put there, and the reader has to know or guess its type. Jackson's databind layer knows how to turn a typed object into JSON tokens, the stream of start object, field name, value and end object events that a JSON document consists of, and how to build a typed object from such tokens. A map is just another object it can read from or write to.
2Converting the Map to a Customer and Back
convertValue takes a source value and a target type. For a plain class we pass the Class; for a generic target such as a Map<String, Object> we pass a TypeReference, an anonymous subclass that keeps the generic type information that Java would otherwise erase.
Notice two things in the output. The key vip came back as vip, not important, because @JsonProperty applies during conversion exactly as it would for JSON. And birthDate is a String in the resulting map, not a LocalDate, because a map holds what JSON would hold.
3How convertValue Works Inside
convertValue does not produce a JSON string, but it does follow the same path as serialization followed by deserialization. Jackson serializes the source into a TokenBuffer, an in-memory list of JSON tokens, and then deserializes the target from that buffer. That is why every annotation, naming strategy, custom serializer and custom deserializer applies, and why the result is the same as writing JSON and reading it back, minus the text.
6 messages in order. Play them, or step through them one at a time.
No JSON text is produced, but serialization and deserialization rules both apply.
So convertValue is cheaper than going through a String, because there is no character encoding and no parsing of text, but it is not free: it still walks every property twice. For hot paths with large objects that is worth measuring. It also means conversion errors look like deserialization errors: a missing creator or a wrong type throws IllegalArgumentException wrapping the underlying Jackson exception.
4The Surprising Types in Maps
The other direction, converting an object into a Map<String, Object>, is where consumers get surprised. Jackson chooses the smallest natural Java type for each JSON value. Whole numbers that fit in an int become Integer, larger ones become Long, and beyond that BigInteger. Decimal numbers become Double unless USE_BIG_DECIMAL_FOR_FLOATS is enabled. Dates become whatever their JSON form is: a String when timestamps are disabled, or numbers and even lists when they are enabled.
So a consumer that casts m.get("id") to Long gets a ClassCastException, and one that reads amount loses the exact value. If the map goes to another system, such as a message header or a document store, these types are what it will store. The safe habit is to read values from such maps through Number and its longValue(), or better, to convert the map into a typed object as soon as it enters our code and work only with that.
5The Tree Model Equivalents
When the generic shape is a JsonNode rather than a map, the same idea has dedicated methods. valueToTree turns a POJO into a JsonNode, and treeToValue turns a JsonNode into a POJO. They also go through a token buffer, and they keep numbers more precisely than a map can, because the tree has distinct node types for ints, longs and decimals.
In Jackson 3 these methods keep their names on the immutable mapper, but the exceptions become unchecked, so code that caught JsonProcessingException around treeToValue simplifies.
6Follow-up questions
Why not just cast the values out of the map by hand?
Because we would repeat type handling Jackson already does, without its annotations, naming rules or validation. Hand-written casting also breaks on the number surprises: a value that was a Long on the producer side may arrive as an Integer. convertValue applies one consistent mapping and fails with a clear error when the shape is wrong.
Does convertValue respect FAIL_ON_UNKNOWN_PROPERTIES?
Yes. Because the target is deserialized normally, a key in the map without a matching property fails if that feature is enabled, and is ignored if it is disabled. It is a good idea to use the same mapper configuration for conversion as for the rest of the application so behaviour matches.
Is convertValue faster than writeValueAsString followed by readValue?
Usually yes, because it skips producing and parsing text. It still does full serialization and deserialization work, so for very hot paths it may be worth designing the code to work with typed objects from the start instead of converting.
7Conclusion
In this answer, we converted a Map<String, Object> into a Customer with mapper.convertValue(map, Customer.class) and back with a TypeReference<Map<String, Object>>. We saw that convertValue goes through a token buffer, so annotations and custom serializers apply, that maps hold Integer, Long or Double numbers and string or numeric dates that may surprise consumers, and that valueToTree and treeToValue do the same between JsonNode and POJOs.
Open with
I would use convertValue on the application's ObjectMapper. Map to object is mapper.convertValue of the map with Customer.class, and object to map is convertValue of the customer with a TypeReference of Map of String to Object, so the generic type is not lost.
Then, if they want more
No JSON string is involved, but internally Jackson serializes the source into a TokenBuffer and deserializes from it, so all the annotations, naming strategies and custom serializers apply just as they would for JSON, and errors come back as IllegalArgumentException wrapping the Jackson error. The thing I would point out is the types in the resulting map.
Numbers become the smallest natural type, so a long id of 42 comes back as an Integer and a cast to Long fails, decimals become Double unless USE_BIG_DECIMAL_FOR_FLOATS is on, so 12.50 becomes 12.5, and dates become whatever their JSON form is, ISO strings, or numbers and lists when timestamps are enabled.
So I convert to typed objects as early as possible. If the generic form is a JsonNode instead of a map, valueToTree and treeToValue are the equivalents, and they keep number types more precisely.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Convert a Map<String, Object> received from a message queue into a typed Customer object...": https://scrivio-interview-library.netlify.app/spring-boot/json-serialization/aa08f858d2/