Is it possible to **update** a key name in a JsonNode once it has been instantiated? My research shows that I need to create a *new* key and put in the node with the old value i.e. updating just the key is not possible.
Example: I have a nested JSON document I'm parsing with Jackson. I would like to rename keys in a nested node e.g. imagine if I have a Person who is represented like so:
{"name":"Bilal", "cat":{"name":"Mr. Bigglesworth", "grumpy":true }}
I want to change cat.grumpy to cat.hateful
{"name":"Bilal", "cat":{"name":"Mr. Bigglesworth", "hateful":true}}
The way I am doing it right now seems inefficient:
1) Person.java is a bean with Jackson annotations e.g.
class Person {
private String personName;
private JsonNode body;
// getters, setters ommitted
}
2) Instantiate Person using an ObjectMapper.readvalue(parser, Person.class)
3) In the setBody method, I recursively iterate through keys in the "cat" JsonNode, creating new keys (e.g. "hateful") and putting them in the JsonNode with new key names.
One downside of my approach is that node order changes (yes, I know JSON nodes aren't ordered ... but it's still an annoying side-effect)