Convert Java Object to JsonNode in Jackson

Max Schmidt picture Max Schmidt · Aug 6, 2012 · Viewed 145.8k times · Source

Is it possible to directly convert a Java Object to an JsonNode-Object?

The only way I found to solve this is to convert the Java Object to String and then to JsonNode:

ObjectMapper mapper = new ObjectMapper(); 
String json = mapper.writeValueAsString(object);
JsonNode jsonNode = mapper.readTree(json);

Answer

Max Schmidt picture Max Schmidt · Aug 6, 2012

As of Jackson 1.6, you can use:

JsonNode node = mapper.valueToTree(map);

or

JsonNode node = mapper.convertValue(object, JsonNode.class);

Source: is there a way to serialize pojo's directly to treemodel?