We have a JSON object given below.
{"label":"Country","value":"1","children":[{"label":"USA","value":"2","children":[{"label":"AZ","value":"3","children":[{"label":"Tempe","value":"4","children":[{"label":"Cotton","value":"5","children":[{"label":"Lake","value":"6","leaf":true},{"label":"Sweet","value":"7","leaf":true}]},{"label":"Sun","value":"9","leaf":true}]},{"label":"Mesa","value":"11","leaf":true}]},{"label":"LV","value":"13","children":[{"label":"Casino","value":"17","leaf":true},{"label":"Fountain","value":"19","leaf":true}]}]},{"label":"Mexico","value":"21","children":[{"label":"Guad","value":"7","leaf":true},{"label":"Puerto","value":"3","children":[{"label":"Beach","value":"4","leaf":true},{"label":"Sand","value":"5","leaf":true}]}]}]}
Need to iterate in Java in such a way that for the object whose "leaf" attribute is TRUE, we have to take the "id" attribute to check if it's an ODD number or not.
If it is an ODD number then add
"children": [
{
"label": "Fish",
"value": "420"
},
{
"label": "Duck",
"value": "420"
}
]
to that object.
So as per this logic, the JSON will become
{"label":"Country","value":"1","children":[{"label":"USA","value":"2","children":[{"label":"AZ","value":"3","children":[{"label":"Tempe","value":"4","children":[{"label":"Cotton","value":"5","children":[{"label":"Lake","value":"6","leaf":true,"children":[{"label":"Fish","value":"420"},{"label":"Duck","value":"420"}]},{"label":"Sweet","value":"7","leaf":true,"children":[{"label":"","value":""}]}]},{"label":"Sun","value":"9","leaf":true,"children":[{"label":"","value":""}]}]},{"label":"Mesa","value":"11","leaf":true,"children":[{"label":"","value":""}]}]},{"label":"LV","value":"13","children":[{"label":"Casino","value":"17","leaf":true,"children":[{"label":"","value":""}]},{"label":"Fountain","value":"20","leaf":true,"children":[{"label":"Fish","value":"420"},{"label":"Duck","value":"420"}]}]}]},{"label":"Mexico","value":"21","children":[{"label":"Guad","value":"7","leaf":true,"children":[{"label":"","value":""}]},{"label":"Puerto","value":"3","children":[{"label":"Beach","value":"4","leaf":true,"children":[{"label":"Fish","value":"420"},{"label":"Duck","value":"420"}]},{"label":"Sand","value":"5","leaf":true,"children":[{"label":"","value":""}]}]}]}]}