3

Need help with transforming a recursive json object into another json object.

My input object looks like this:

{  
  person: 
  {
       name: 'John Henry',
       age: 63
  },
  children: 
  [
        {
            person:
            {
                name: 'Paul Henry',
                age: 40
            },
            children: 
            [
                {
                    person:
                    {
                        name: 'Tom Henry',
                        age: 10
                    }
                }
                {
                    person:
                    {
                        name: 'Mike Henry',
                        age: 12
                    }
                }
            ]
        },
        {
            person:
            {
                name: 'Wilson Henry',
                age: 30
            }
        },
        {
            person:
            {
                name: 'Richard Henry',
                age: 59
            }
        }
  ]
}

The output I want is:

[{
          name: 'John Henry',
          attributes: { age: 63 },
          children: 
          [
            {
                name: 'Paul Henry',
                attributes: { age: 40 },
                children: [
                  {
                      name: 'Tom Henry',
                      attributes: { age: 10 }
                  },
                  {
                      name: 'Mike Henry',
                      attributes: { age: 12 }
                  }
                ]
            },
            {
                name: 'Wilson Henry',
                attributes: { age: 30 }
            },
            {
                name: 'Richard Henry',
                attributes: { age: 59 }
            }
         ]
}];

This is what I have tried so far, but got stuck at the recursion part. Not sure how to bring everything together.:

var tree = {};
var getFamilyTree = function (input) {

  tree.name = input.person.name;
  tree.attributes = { 'age': input.person.age };
  tree.children = [];

  input.children.forEach(function (child) {
      //not sure how to recursively go through the child nodes.
  });
};

1 Answer 1

3

Create a new output object at the start of the function.

For each child, call getFamilyTree() again on the child - it will be the new input. Append the result of that call to your new children list.

Return your newly-created object.

Something like:

function getFamilyTree(input)
{
  var newb = { 'attributes': {} };

  for ( f in input.person )    // we may have attributes other than "age"
    if (f == 'name')
      newb.name = input.person[f];
    else
      newb.attributes[f] = input.person[f];

  if (input.children && input.children.length)
    {
      newb.children = [];

      for ( var i = 0; i < input.children.length; ++i )
        {
          newb.children.push(getFamilyTree(input.children[i]));
        }
    }

  return newb;
}

var orig = {  
  person: 
  {
       name: 'John Henry',
       age: 63
  },
  children: 
  [
        {
            person:
            {
                name: 'Paul Henry',
                age: 40
            },
            children: 
            [
                {
                    person:
                    {
                        name: 'Tom Henry',
                        age: 10
                    }
                },
                {
                    person:
                    {
                        name: 'Mike Henry',
                        age: 12
                    }
                }
            ]
        },
        {
            person:
            {
                name: 'Wilson Henry',
                age: 30
            }
        },
        {
            person:
            {
                name: 'Richard Henry',
                age: 59
            }
        }
  ]
};


function getFamilyTree(o)
{
  var newb = { 'attributes': {} };
  
  for ( f in o.person )    // we may have attributes other than "age"
    if (f == 'name')
      newb.name = o.person[f];
    else
      newb.attributes[f] = o.person[f];
  
  if (o.children && o.children.length)
    {
      newb.children = [];
      
      for ( var i = 0; i < o.children.length; ++i )
        {
          newb.children.push(getFamilyTree(o.children[i]));
        }
    }
  
  return newb;
}


console.log( JSON.stringify( getFamilyTree(orig), null, "  " ) );

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.