42

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!!

RAM
  • 2,257
  • 2
  • 19
  • 41
Mr. Noddy
  • 1,530
  • 2
  • 16
  • 45
  • 1
    Chances are you are trying to convert the value of ObjectNode to an objectNode. ObjectNode means {"test":"value"} and you are probably typecasting value node. If you can share the code, may be suggestions can be made on which node you should cast – Optional Sep 22 '15 at 09:27

5 Answers5

56

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

Andrea Bergonzo
  • 3,983
  • 4
  • 19
  • 31
Daniele Licitra
  • 1,520
  • 21
  • 45
35

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!

Mr. Noddy
  • 1,530
  • 2
  • 16
  • 45
  • 4
    I think you are supposed to call JsonNode.isObject() to make sure you can cast before casting. – romnempire Apr 24 '19 at 18:42
  • 1
    Definitely, it's worth checking the type before. Imagine that incoming String is not an expected JSON value but literally, a number f.ex "737". Then readTree() would convert it into IntNode object which cannot be cast into ObjectNode what will result with ClassCastException. – Macieyo Apr 25 '19 at 10:07
  • 1
    I'm facing the similar problem, my string is coming as a number "71" and readTree() method is converting it into IntNode object and it is throwing ClassCastException. How to convert IntNode into ObjectNode in that case? – Rachit Munjal Jan 14 '21 at 11:36
  • Add the following import for the above to work, import com.fasterxml.jackson.databind.node.ObjectNode; – Anurag Bhalekar Sep 19 '22 at 06:02
5

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.

dozer
  • 861
  • 1
  • 11
  • 22
0

You can covert JsonNode to ObjectNode this way:

JsonNode demoPath = requestParams.at("/Subscription/0");
((ObjectNode) demoPath).put("mylID", "test12");
Testilla
  • 602
  • 8
  • 21
-2

I try it some times, it will be ok! You only define a Student Class to map the properties. Then you could convert the jsonNode to Student Object.

Student student = objectMapper.convertValue(jsonNode1, Student.class);

I think this will be suitable for your need!

31piy
  • 23,323
  • 6
  • 47
  • 67