I am trying to convert below object to array of string
JSON Object
Input :
[
{
"name": "Pantry",
"childrenItems": [
{
"name": "Butter",
"childrenItems": [
{
"name": "Cream",
"childrenItems": []
}
]
},
{
"name": "Snack",
"childrenItems": []
}
]
},
{
"name": "Medicine",
"childrenItems": []
}
]
Required Output:
[ "Pantry->Butter->Cream", "Pantry->Snack", "Medicine" ]
My POJO looks like this
@Data
public class CategoryTreeDto {
private String name;
private List<CategoryTreeDto> childrenItems;
}
How can I flatten the JSON object of Nested categories using java 8 stream API.
I tried using the recursion and java 8 flatMap function to flatten and concatenate the strings but not getting output as expected.
It is based on parent child relationship, as pantry is a parent and its child is butter and again butter's child is cream and also pantry has another child which is snack.