How to convert JsonNode to ObjectNode

I had this error too although in my case it was a stupid mistake. I accidentally imported org.codehaus.jackson.node.ObjectNode instead of com.fasterxml.jackson.databind.node.ObjectNode. Using the jackson ObjectNode fixed the issuse.


Finally, I got the solution as follows...

JsonNode jsonNode = Json.toJson("Json String");
ObjectNode node = (ObjectNode) new ObjectMapper().readTree(jsonNode.asText());
//perform operations on node
jsonNode = (JsonNode) new ObjectMapper().readTree(node.toString());

or another one as below...

ObjectNode node = (ObjectNode) new ObjectMapper().readTree("Json String")
//perform operations on node
jsonNode = (JsonNode) new ObjectMapper().readTree(node.toString());

but I don't know if this is good approach or not ? If there is any better than above, please let me know. Thank you!


You can convert a JsonNode in an ObjectNode in this simple way:

ObjectNode objectNode = jsonNode.deepCopy();

Available from Jackson 2.0 and tested with Jackson 2.4.0

Tags:

Java

Json

Jackson