I have a HashMap
object which I want to convert to JsonNode
tree using com.fasterxml.jackson.databind.ObjectMapper
. What is the best w
The following will do the trick:
JsonNode jsonNode = mapper.convertValue(map, JsonNode.class);
Or use the more elegant solution pointed in the comments:
JsonNode jsonNode = mapper.valueToTree(map);
If you need to write your jsonNode
as a string, use:
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
First transform your map in a JsonNode :
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNodeMap = mapper.convertValue(myMap, JsonNode.class);
Then add this node to your ObjectNode with the set method :
myObjectNode.set("myMapName", jsonNodeMap);
To convert from JsonNode to ObjectNode use :
ObjectNode myObjectNode = (ObjectNode) myJsonNode;