How AI is applied across API Evangelist and APIs.io. Read my AI disclosure →
API Evangelist API Evangelist
Discovery
Learnings
Guidance
Toolbox
Alignment
API Evangelist LLC

How to convert a flat parent-child list from database into a nested tree structure for JSON response in Spring Boot?

calendar_today May 27, 2026 person Oscar Sandoval domain spring-boot

When calling my API endpoint, the default repository output returns a flat list array like this [ { “id”: “root_postgres”, “name”: “Main Root”, “type”: “FOLDER”, “parentId”: null }, { “id”: “exams_folder”, “name”: “Exams”, “type”: “FOLDER”, “parentId”: “root_postgres” } ] But i want a deeply neste JSON tree structure like this { “id”: “root_postgres”, “value”: “Main Root”, “children”: [ { “id”: “exams_folder”, “value”: “Exams”, “children”: [] } ] } What is the cleanest algorithmic approach or architectural pattern in a Spring @Service to index these database rows dynamically, preferably in O(1

open_in_new Read original post