I am working with a basic database schema that includes parent-child relationships spanning 2 to 3 levels deep. An example of this is:
- School has many teachers
- School has many departments
Currently, my query produces a flat table output like this:
school teacher department
CMU John Engineering
CMU Julie Engineering
CMU John Humanities
CMU Julie Humanities
While ORM tools can return results as objects, I find them too cumbersome for my needs. Are there any straightforward algorithms or libraries available to transform this result into hierarchical object structures, perhaps in JSON format like this:
{
departments: [
'Engineering',
'Humanities'
],
teachers: [
'John',
'Julie'
]
}
This could also be extended for a 3-level deep relationship with a different query and result set:
{
departments: [
{
name: 'Engineering',
teachers: [
'John',
'Julie'
]
},
{
name: 'Humanities',
teachers: [
'Mike',
'Mary'
]
}
]
}