How do you create a JSON family tree

Hi All,
I would be very grateful if you could show how to create a JSON style data structure with the following attributes;

The structure would be a recursive structure like this

family ={
 name : "name of person",
 children : [{
    name : "name of person",
    children: [{
      children:[ ]

 specifically i would like to call an initator function that takes as arguments
the "name of the person" and the family_tree as an object;

so lets say the family tree is something like this
Micheal (Grand Father)
Joseph  (Father)
Junior   (Son)

the function would work something like this
var myfamily_tree = new family_tree()


then could i access the tree something like this
myfamilytree.pop() = "Michael"
myfamilytree.pop() = "Joseph"
myfamilytree.pop() = "Junior"

Thanks in advance,
All help is much appreciated
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

To "start" :

    function family_tree(name) {
        this.createFamily = function(name) {
   = name;
            this.childrens = [];
        this.add_node = function(name) {
            return this.childrens[this.childrens.push(new family_tree(name))-1];

    var f = new family_tree("Simpson");
    var Abraham_ft = f.add_node("Abraham");
    var Homer_ft = Abraham_ft.add_node("Homer");

// checking :
    alert( JSON.stringify(f) );

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JITchallengerAuthor Commented:
Thanks leakim971 for that excellent code
Your definitely the champ here
<a href=""> Hey Ha Hoo Hey</a>

// try this which includes your pop functionality:

function familyMember( name ) { = name;

  this.children = [];

  this.addChild = function( member ) {
    this.children.push( member );

  this.pop = function( name ) {
    if ( name == ) return this;
    for ( var index = 0; index < this.children.length; index++ ) {
      var child = this.children[index];
      if ( name == child.pop( name ).name ) return child.pop( name );

  this.toString = function() { return JSON.stringify( this ); };


var familyTree = new familyMember( "Michael" );
var joseph = new familyMember( "Joseph" );
familyTree.addChild( joseph );

var junior = new familyMember( "Junior" );
joseph.addChild( junior );

alert( familyTree.pop("Junior").toString() );
You're welcome!
Have a nice sunday and Happy New Year!
this.pop = function() {
            return this.childrens.pop().name;

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.