Q:

How to convert JsonNode to ObjectNode

I have a com.fasterxml JsonNode object with some data. I need to do some manipulation on its data. I googled for answer but didn't got it properly. Can you please suggest me how to manipulate JsonNode data. 
I have also tried to convert JsonNode to ObjectNode as follows
ObjectNode objectNode = (ObjectNode)filterJson;

but its giving following exception....
java.lang.ClassCastException: com.fasterxml.jackson.databind.node.TextNode cannot be cast to 
com.fasterxml.jackson.databind.node.ObjectNode

please help!!

A:

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

A:

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! 

A:

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.

