Convert the flat Java Object that was created from the DB read, into nested Json object

Viewed 171

The threaded comments are stored in Database as flat records with commenId and ParentCommentId. Like below.

    commentId : 1
    userId    : 815
    userFName:Joe
    userLName:Doe
    timeStamp:12345678888
    commentText:""
    parentCommentId:0

    commentId : 2
    userId    : 615
    userFirstName:Ken
    userLastName:Tait
    timeStamp:12345678988
    commentText:"Comment text"
    parentCommentId:1

    commentId : 3
    userId    : 415
    userFirstName:Brain
    userLastName:Dell
    timeStamp:12345678
    commentText:"Comment text"
    parentCommentId:0

I build the Java object using the following Java class

    public class Comment {

        int commentId;
        int userId;
        String userFName;
        String userLName;
        long timeStamp;
        String commentText;
        int parCommId;
    }

    List<Comment> comments;

I have the List of comments object. Now I have to traverse the list and convert this list of comment object into nested Json object. The comment objects with parCommId == 0 are the top level comment and the other comment objects (parCommId != 0) should be nested under the commentId of the comment object.

In the above example, the output should be nested like below

    CommentId_1
         CommentId_2
    CommentID_3
1 Answers

As suggested in the comments, let's add a List<Comment> field in Comment.class.

Then, assuming the input from the DB is:

List<Comment> comments = Arrays.asList(
        new Comment(1, 6, "John", "Snow", 0, "asd", 0),
        new Comment(2, 6, "Tif", "Snow", 0, "asd2", 1),
        new Comment(3, 6, "Yur", "Snow", 0, "asd", 2),
        new Comment(4, 6, "Mrr", "Snow", 0, "asd", 0),
        new Comment(5, 6, "Mrr", "Snow", 0, "asd", 2)
);

You can do the following:

Map<Integer, List<Comment>> parentToComments = comments.stream()
            .collect(Collectors.groupingBy(Comment::getParCommId));

comments.forEach(comment -> {
    List<Comment> children = parentToComments.get(comment.getCommentId());
    comment.setChildren(children);
});

ObjectMapper objectMapper = new ObjectMapper();
String commentsJson = objectMapper.writeValueAsString(parentToComments.get(0));

Output:

[{
    "commentId": 1,
    "userId": 6,
    "userFName": "John",
    "userLName": "Snow",
    "timeStamp": 0,
    "commentText": "asd",
    "parCommId": 0,
    "children": [{
        "commentId": 2,
        "userId": 6,
        "userFName": "Tif",
        "userLName": "Snow",
        "timeStamp": 0,
        "commentText": "asd2",
        "parCommId": 1,
        "children": [{
            "commentId": 3,
            "userId": 6,
            "userFName": "Yur",
            "userLName": "Snow",
            "timeStamp": 0,
            "commentText": "asd",
            "parCommId": 2,
            "children": null
        }, {
            "commentId": 5,
            "userId": 6,
            "userFName": "Mrr",
            "userLName": "Snow",
            "timeStamp": 0,
            "commentText": "asd",
            "parCommId": 2,
            "children": null
        }]
    }]
}, {
    "commentId": 4,
    "userId": 6,
    "userFName": "Mrr",
    "userLName": "Snow",
    "timeStamp": 0,
    "commentText": "asd",
    "parCommId": 0,
    "children": null
}]
Related