I have a list of objects, of type TDepartment which looks like this
TDepartment = class
ID : Integer;
Name : string;
ParentDepartmentID : Integer;
end;
I need to create a TJSONObject, with an array of departments, which all can also have an array of departments. So the depth of this is unknown.
I am at a point right now where it simply doesn't make sense to me, but I would like the resulting JSON to look like this:
"department_id": "5",
"department_name": "100",
"parent_dept_id": "",
"subdepartments": [{
"department_id": "8",
"department_name": "300",
"parent_dept_id": "5",
"subdepartments": [{
"department_id": "1",
"department_name": "310",
"parent_dept_id": "8",
"subdepartments": []
Keep in mind that each level has unknown amount of siblings and children. I guess i need to write a recursive procedure, but I am unable to visualize it.