This is the input:

```
[{
    "PSpace": "D1",
    "Category": "C1",
    "SubCategory": "S1",
    "Pname": "P1"
}, {
    "PSpace": "D1",
    "Category": "C2",
    "SubCategory": "S2",
    "Pname": "P2"
}, {
    "PSpace": "D1",
    "Category": "C2",
    "SubCategory": "S3",
    "Pname": "P6"
}, {
    "PSpace": "D2",
    "Category": "C6",
    "SubCategory": "S7",
    "Pname": "P7"
}, {
    "PSpace": "D2",
    "Category": "C6",
    "SubCategory": "S7",
    "Pname": "P8"
}]

```

Desired Output

In a single array each Department should have an array of related Category, each Category should have an array of related Subcategory and each Subcategory should have an array of related Products

so that i can iterate through a single array an display products as attached 

[![enter image description here](https://i.stack.imgur.com/FuFMI.png)](https://i.stack.imgur.com/FuFMI.png)