I've used Jackson off and on for a while, so I'm familiar with it, but it has been a while and I find myself facing a little issue that I feel should be simpler than what I'm seeing, but I could be wrong.
I have a JSON string in Java. I need to find all occurrences of a particular key within that string. This key could pop up in a number of places, either in the root object, subobject, or an array of objects with who knows how much nesting. Basically, I need to find all instances of this key (along with the value), rename the key, and change the value. It's also possible that this key will not exist at all, depending on the string.
Basically, I need to find key "a", but I could have any of the following:
{b: 3, c: [{a: 0},{a: 7}]}
{a: 5, c: [], f: {a: 12}}
You get the idea. The structure has some variability to it, but in all cases I need to find all (if any) occurrences of this key and make the needed changes. Is there a simple way to do this? In a nutshell, I know that I could do something like this:
Map<String, Object> map = objectMapper.readValue(...);
And iterate through the Map, typechecking, recursing, etc as needed, but that seems to be an excessive amount of code for this. I know I could do something similar with ObjectMapper.readTree()
, but then I'd be doing a pretty similar operation (typecheck, iterate, recurse), perhaps with a little less code, but still bulky. I feel like there should be a simpler way, but maybe I'm mistaken.