0

Running into problem while trying to generate json using recursive cte

My structure contains id and parent_id on the same table. The id is tie to the parent_id, and I won't know how deep the structure would go.

Here's the table and code I have created https://dbfiddle.uk/DFCYOzUm

I did a fair amount of research and this is the closest solution I have found:

Recursively generate JSON tree from hierarchical table in Postgres and jOOQ

this is what I am getting

{
        "id": "1",
        "children": [
            {
                "id": "1.1",
                "children": [
                    {
                        "id": "1.1.2",
                        "children": [
                        ],
                        "unit_name": "C",
                        "randominfo": "C"
                    }
                ],
                "unit_name": "C",
                "randominfo": "C"
            }
        ],
        "unit_name": "A",
        "randominfo": "A"
    },
    {
        "id": "1",
        "children": [
            {
                "id": "1.1",
                "children": [
                    {
                        "id": "1.1.1",
                        "children": [
                            {
                                "id": "1.1.1.1",
                                "children": [
                                    {
                                        "id": "1.1.1.1.1",
                                        "children": [
                                        ],
                                        "unit_name": "C",
                                        "randominfo": "C"

This the structure I am looking for, I want 1.1.2 and 1.1.1 to be under 1.1 :

{
        "id": "1",
        "children": [
            {
              
              {
                "id": "1.1",
                "children": [
                    {
                        "id": "1.1.1",
                        "children": [
                            {
                               "id": "1.1.1.1"      
                               "children": [}
                            },
                           {
                               "id": "1.1.1.2"      
                               "children": [}
                            }
                        ],
  
                    },
                  { 
                        id": "1.1.2",
                       "children" []
                  }
                ],

            }
            }
        ]
}```


Any help would be appreciated!
4
  • Why do you expect to see a result with id 1.2 if this id is not in your data? Commented Dec 28, 2023 at 13:48
  • @MarmiteBomber 1.2 is a typo and thanks for pointing it out. I went ahead and updated my question, basically I want both 1.1.2 and 1.1.1 to be under 1.1 and not split into two. Commented Dec 29, 2023 at 1:31
  • You'll need to show us the SQL code you're using to produce that result, we cannot tell you what's wrong and what to adjust otherwise Commented Dec 29, 2023 at 1:51
  • @Bergi here it is. dbfiddle.uk/DFCYOzUm Commented Dec 29, 2023 at 2:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.