I have a JSON data structure that I need to transform into a different format.
The original JSON format:
- values = an array containing objects that need to be filtered by action === 'commented'
- comment = an object with the comment, n Tasks, and n Comments
- Comments can have an unlimited number of nested Comments and Tasks
{
"values": [
{
"action": "COMMENTED",
"comment": {
"text": "comment text",
"comments": [
{
"text": "reply text",
"comments": [],
"tasks": []
}
],
"tasks": [
{
"text": "task text",
"state": "RESOLVED"
}
]
}
}
]
}
The desired Target JSON format:
- Arrays with Objects
- each comment or task is a "children" (recursive!)
[
{
"text": "comment text",
"children": [
{
"text": "reply text",
"type": "comment"
},
{
"text": "task text",
"state": "RESOLVED"
}
]
}
]
I have started working on it using the following code snippet:
data = data.values.filter((e)=>{
return e.action === 'COMMENTED';
}).map((e)=>{
// Need to implement recursion here, any ideas?
});