Convert a Java object to JsonNode in Jackson
Codemia Knowledge Hub ·
Use ObjectMapper.valueToTree(Object). It converts a Java object directly into Jackson's JsonNode, the tree model that represents a JSON structure, without going through a JSON string. Jackson is one of the most popular JSON libraries for Java, and its ObjectMapper handles conversion between Java objects, JSON text and the tree model.
What JsonNode is
JsonNode is part of Jackson's Tree Model, which represents JSON as a tree of node objects. It is an abstract class with concrete implementations such as ObjectNode and ArrayNode. The tree model is useful when the JSON structure is unknown, or when you prefer to manipulate JSON as a tree.
Converting an object with valueToTree
- ObjectMapper: Jackson's core class.
valueToTree(Object fromValue)converts an object to aJsonNode. - JsonNode: a node in a JSON tree.
Because JsonNode does not need a schema, it handles varying JSON structures easily, and you can traverse and manipulate the data like any tree.
Reading fields from the JsonNode
Once the object is a JsonNode, read values with get() and convert them with methods such as asText() and asInt():
Adding and removing fields
To add or remove fields dynamically, cast the JsonNode from the first example to ObjectNode, the concrete type for JSON objects:
Serializing the JsonNode back to a string
To turn the tree back into JSON text, pass it to writeValueAsString:
Quick reference
| Feature | Description | Code Example |
| Basic Conversion | Converts a Java object to JsonNode | objectMapper.valueToTree(user) |
| Accessing Data | Access and read values from JsonNode | rootNode.get("firstName").asText() |
| Modifying JsonNode | Adding/removing fields | objectNode.put("email", ...); objectNode.remove("age") |
| Serializing JsonNode | Convert JsonNode back to JSON string | objectMapper.writeValueAsString(jsonNode) |
The tree model gives you flexibility with complex or unpredictable JSON, whether you are serializing, deserializing or modifying data.
Related reading
- Convert java.time.LocalDate into java.util.Date type
- Convert java.util.Date to java.time.LocalDate
- Convert java.util.Date to String
- Convert JSON String to Pretty Print JSON output using Jackson
- Convert JsonNode into POJO
- Convert JWT Authentication Principal to something more usable in spring
- Convert Kotlin Array to Java varargs
- Convert list to array in Java
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.