How to get comment details parent comment and child comment

  "posts": [
    {
      "postId": "3675196306686547",
      "comments": [
        {
          "commentId": "1",
          "userId": "user123",
          "content": "This is the parent comment.",
          "timestamp": "2024-01-21 12:34:56",
          "childComments": [
            {
              "commentId": "2",
              "userId": "user456",
              "content": "This is a child comment.",
              "timestamp": "2024-01-21 12:35:00",
              "childComments":[ {
                "commentId": "3",
                "userId": "user789",
                "content": "Another child comment.",
                "timestamp": "2024-01-21 12:36:00",
                "childComments":[ {
                  "commentId": "3",
                  "userId": "user789",
                  "content": "Another child comment.",
                  "timestamp": "2024-01-21 12:36:00",
                  
                }]
              }]
            }
          ],
          user:{
            name:"John Doe"
          }
        }
      ]
    }
  ]
} this type reponse how to get details 
![Screenshot 2024-01-21 153540|690x387](upload://iWqee6QoWIM5sbvVxlGXCRIGzVG.png)




Cypher does not have a recursive construct in order to easily created a hierarchical list of items. I did figure out an approach using the "reduce" list operation.

Note: I did not wrap the comments in arrays, as you are not returning an array of comments since each is embedded as a property of the previous. As such, the post has one comment, and each child comment has one comment. You can modify the code to just wrap each comment in a list, thus returning a list of one element each.

Test data:

create(p:Post{postId:0})
create(c0:Comment{id:100,content:"This is the parent comment.",timestamp: "2024-01-21 12:34:56"}),(c1:Comment{id:101,content: "This is a child comment.",timestamp: "2024-02-21 12:34:56"}),(c2:Comment{id:102,content: "This is a child comment.",timestamp: "2024-03-21 12:34:56"}),(c3:Comment{id:103,content: "This is a child comment.",timestamp: "2024-04-21 12:34:56"})
create(c0)<-[:REPLIED_TO]-(c1)<-[:REPLIED_TO]-(c2)<-[:REPLIED_TO]-(c3)
create(p)<-[:POST_COMMENT]-(c0)

Query:

match(post:Post{postId:0})<-[:POST_COMMENT]-(n:Comment)
match p=(n)<-[:REPLIED_TO*0..]-(m:Comment)
where not exists((m)<-[:REPLIED_TO]-(:Comment))
with post, properties(n) as comment, reverse(nodes(p)[1..]) as children
return {
    postId: post.postId,
    comments: comment{.*, childComments: reduce(s=properties(head(children)), i in tail(children)|i{.*, childComments:s})}
}

Result:

{
  "comments": {
    "id": 100,
    "childComments": {
      "id": 101,
      "childComments": {
        "id": 102,
        "childComments": {
          "id": 103,
          "content": "This is a child comment.",
          "timestamp": "2024-04-21 12:34:56"
        },
        "content": "This is a child comment.",
        "timestamp": "2024-03-21 12:34:56"
      },
      "content": "This is a child comment.",
      "timestamp": "2024-02-21 12:34:56"
    },
    "content": "This is the parent comment.",
    "timestamp": "2024-01-21 12:34:56"
  },
  "postId": 0
}

i want this type response how to get



  "results" : [  {
        "comments": {
            "text": "parent",
            "createdAt": "Fri Jan 19 2024 17:41:56 GMT+0530 (India Standard Time)",
            "deletedAt": "",
            "publicId": "3577041529831424",
            "postId": "3675196306686547",
            "userInfo":{  "who is comment" },
            "children": [{
                "text": "child1",
                "createdAt": "Fri Jan 19 2024 17:42:55 GMT+0530 (India Standard Time)",
                "userId": "3575189734199296",
                "deletedAt": "",
                "publicId": "3577041652428800",
                "commentId": "3577041529831424",
                "userInfo":{  "who is comment"   },
                "children": [{
                    "text": "child2",
                    "createdAt": "Fri Jan 19 2024 17:44:00 GMT+0530 (India Standard Time)",
                    "userId": "9995055351656459",
                    "deletedAt": "",
                    "publicId": "3577041789575168",
                    "commentId": "3577041652428800",
                    "userInfo":{  "who is comment"   }

                }]
                
            }]
        }
    }
]
}``` **i want this type response  how to get**

your query reponse

    "comments": {
        "childComments": {
            "childComments": {
                "text": "child2",
                "createdAt": "Fri Jan 19 2024 17:44:00 GMT+0530 (India Standard Time)",
                "userId": "9995055351656459",
                "deletedAt": "",
                "publicId": "3577041789575168",
                "commentId": "3577041652428800"
            },
            "text": "child1",
            "createdAt": "Fri Jan 19 2024 17:42:55 GMT+0530 (India Standard Time)",
            "userId": "3575189734199296",
            "deletedAt": "",
            "publicId": "3577041652428800",
            "commentId": "3577041529831424"
        },
        "text": "parent",
        "createdAt": "Fri Jan 19 2024 17:41:56 GMT+0530 (India Standard Time)",
        "deletedAt": "",
        "publicId": "3577041529831424",
        "postId": "3675196306686547"
    },
    "postId": "3675196306686547"
}```

I don't have all the metadata for each node, so the additional properties are missing from my simulation. They will be included when you use real data.

Ok, I added some "lists" to duplicate the structure of your desired request.

I can't control the order of the individual key/value pairs. There is an apoc method that will sort the keys alphabetically.

match(post:Post{postId:0})<-[:POST_COMMENT]-(n:Comment)
match p=(n)<-[:REPLIED_TO*0..]-(m:Comment)
where not exists((m)<-[:REPLIED_TO]-(:Comment))
with post, properties(n) as comment, reverse(nodes(p)[1..]) as children
return {results: [{
    postId: post.postId,
    comments: comment{.*, children: [reduce(s=properties(head(children)), i in tail(children)|i{.*, children:s})]}
}]}
{
  "results": [
    {
      "comments": {
        "children": [
          {
            "children": {
              "children": {
                "id": 103,
                "content": "This is a child comment.",
                "timestamp": "2024-04-21 12:34:56"
              },
              "id": 102,
              "content": "This is a child comment.",
              "timestamp": "2024-03-21 12:34:56"
            },
            "id": 101,
            "content": "This is a child comment.",
            "timestamp": "2024-02-21 12:34:56"
          }
        ],
        "id": 100,
        "content": "This is the parent comment.",
        "timestamp": "2024-01-21 12:34:56"
      },
      "postId": 0
    }
  ]
}

I think you can modify this to meet your needs. The important part is the approach to generating hierarchical data, which I did through the 'reduce' method.

first parent comment come {

  "results" : [  {
        "comments": {
            "text": "parent",
            "createdAt": "Fri Jan 19 2024 17:41:56 GMT+0530 (India Standard Time)",
            "deletedAt": "",
            "publicId": "3577041529831424",
            "postId": "3675196306686547",
            "userInfo":{ name: userB},
            "children": [{
                "text": "child1",
                "createdAt": "Fri Jan 19 2024 17:42:55 GMT+0530 (India Standard Time)",
                "userId": "3575189734199296",
                "deletedAt": "",
                "publicId": "3577041652428800",
                "commentId": "3577041529831424",
                "userInfo":{  name : userA},
                "children": [{
                    "text": "child2",
                    "createdAt": "Fri Jan 19 2024 17:44:00 GMT+0530 (India Standard Time)",
                    "userId": "9995055351656459",
                    "deletedAt": "",
                    "publicId": "3577041789575168",
                    "commentId": "3577041652428800",
                    "userInfo":{  name :userB   }

                }]
                
            }]
        }
    }
]
}    first parent comment then userinfo parentcomment then children then children userinfo then children userinfo   i dont konow how many childrens comment

userinfo also add