Here's the input I have. Some nodes have downlines with multiple other nodes nested inside.
data = [
{
"user_id": "1",
"username": "johndoe001",
"amount": "0.00",
"downlines": [
{
"user_id": "2",
"username": "j001-01",
"amount": "1.00",
"downlines": []...
How can I convert it to the output structure below?
[
{
"key": "1",
"label": "johndoe001 (0.00)",
"nodes": [
{
"key": "2",
"label": "j001-01 (1.00)",
"nodes": []...
I've managed to partially convert it using string replacements, but I'm struggling to update the label value by combining username
and amount
. Additionally, removing unnecessary keys like amount
is posing a challenge.
let stringJson = JSON.stringify(data);
stringJson = stringJson.replace(/downlines/g, 'nodes');
stringJson = stringJson.replace(/username/g, 'label');
stringJson = stringJson.replace(/user_id/g, 'key');
let tmpTree = JSON.parse(stringJson);