I'm a beginning java programmer, so I'm sorry if my question is kind of dumb.
I have a JSON object that looks like this:
{
"element1" : {
"generated_name_1": {
"a" : {"isReady":false}
},
"generated_name_2":{},
"generated_name_3":{},
"generated_name_4":{}
},
"element2" : {
"generated_name_5" : {
"a" : {"isReady":false},
"g" : {"isReady":false}
}
},
"element3" : {
"a" : { "isReady":false},
"n":{}
}
}
I would like to go through and delete every element that has an empty value associated with it, like "generated_name_2" and "n". I have no idea what the names of those elements would be, and I have no idea how far nested into the JSON tree it is.
I get that I have to write a recursive program, and this is what I came up with:
public static void cleanJsonNodes(ObjectNode myJsonNode){
for (JsonNode currentNode : myJsonNode){
if (currentNode.size() == 0){
myJsonNode.remove(currentNode);
} else {
cleanJsonNodes((ObjectNode)currentNode);
}
}
}
Of course, this doesn't work, but I'm not really sure where to go from here and I've scoured the internet to no avail.
Please someone help me!