1

Edit: to be clear, my input data is a JSON object with key-value pairs unlike the other similar posts here and this is the main reason I'm asking this question separately.

I haven't been able to figure out how to turn a flat object into a nested object using either BF, BFS, or DFS. There are multiple examples including arrays, but my case is a bit different due to the structure not being exactly list-like. I'm only doing this transformation due to a third-party library's requirements

Any help would be appreciated!

Input format:

{
    "id_1": {
        "title": "Title 1",
        "parent": null,
    },
    "id_2": {
        "title": "Title 2",
        "parent": "id_1",
    },
    "id_3": {
        "title": "Title 3",
        "parent": "id_2",
    }
}

Desired output format:

{
    "id": "id_1",
    "title": 'Title 1',
    children: [
    {
        "id": "id_2",
        "title": "Title 2",
        children: [
        {
            "id": "id_3",
            title: "Title 3",
            children: []
        }]
    }]
}

1

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.