I am writing a commenting system. For a particular page, I am fetching the the comments as highly nested JSONs (a comment contains its children, that in turn may contain other children). I want to parse and display these comments in a threaded manner. Here's an example of one such JSON:
{
"children": [
{
"children": [
{
"children": [],
"content": "Here is a child8",
"id": "child8",
"pageURIHash": "3988665684",
"parentId": null
}
],
"content": "Here is child1",
"id": "child1",
"pageURIHash": "3988665684",
"parentId": null
},
{
"children": [
{
"children": [],
"content": "Here is child3",
"id": "child3",
"pageURIHash": "3988665684",
"parentId": null
},
{
"children": [],
"content": "Here is child4",
"id": "child4",
"pageURIHash": "3988665684",
"parentId": null
},
{
"children": [],
"content": "Here is a child5",
"id": "child5",
"pageURIHash": "3988665684",
"parentId": null
},
{
"children": [
{
"children": [
{
"children": [],
"content": "Here is a child9",
"id": "child9",
"pageURIHash": "3988665684",
"parentId": null
},
{
"children": [],
"content": "Here is a child10",
"id": "child10",
"pageURIHash": "3988665684",
"parentId": null
}
],
"content": "Here is a child7",
"id": "child7",
"pageURIHash": "3988665684",
"parentId": null
}
],
"content": "Here is a child6",
"id": "child6",
"pageURIHash": "3988665684",
"parentId": null
}
],
"content": "Here is child2",
"id": "child2",
"pageURIHash": "3988665684",
"parentId": null
}
],
"content": "Here is a parent comment",
"id": "parent",
"pageURIHash": "3988665684",
"parentId": null
}
Edit: Ignore the null parentIds. This is just a mock document. Each child comment will have a corresponding parentId, except for the top-level comment
What would be the strategy to solve this problem? I can parse the JSON but how would I display the comments in a threaded fashion? Sorry if this question appears naive but I haven't worked on the front-end much.
Thanks a lot!
Edit: I am using JQuery and Bootstrap. Here's an example of how I want the formatting to be:
https://www.reddit.com/r/AskReddit/comments/a9jcxo/what_are_some_nonobvious_early_signs_that_a/
Edit: Looks like I found a solution. I am using jsTree and it seems to work fine for my use-case.