Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a family tree structure in JSON format [closed]

I am experimenting with JSON format and not sure how to use it to structure a family tree. This is what I have got (to keep it simple am only listing the father, his children and whether those children have any children themselves. I haven't listed spouses' names).

{
    "Name": "Jonathan Smith",
    "Children": [
        {
            "name": "Adam",
            "Children": [
                {
                    "name": "Suzy",
                    "children": ""
                },
                {
                    "name": "Clare",
                    "children": ""
                },
                {
                    "name": "Aaron",
                    "children": ""
                },
                {
                    "name": "Simon",
                    "children": ""
                }
            ]
        },
        {
            "name": "Timmy",
            "Children": ""
        },
        {
            "name": "Alison",
            "Children": [
                {
                    "name": "Natasha",
                    "children": ""
                },
                {
                    "name": "Zak",
                    "children": ""
                }
            ]
        }
    ]
}

Although, it's validates fine, I am not sure if that best way of doing it (i.e. is my approach DRY and scalable for example).

like image 236
Andy Avatar asked Nov 02 '13 13:11

Andy


People also ask

What is [] and {} in JSON?

' { } ' used for Object and ' [] ' is used for Array in json.

How do I structure a JSON file?

Rules for JSON SyntaxData should be in name/value pairs. Data should be separated by commas. Curly braces should hold objects. Square brackets hold arrays.

Is JSON a tree structure?

JSON has three types of nodes, which are Value, Object and Array. We know that JSON nodes have a hierarchical tree structure.

Which type of data structure is suitable for JSON format?

Valid Data Typesan object (JSON object) an array. a boolean. null.


1 Answers

Most simple way:

{
     "Jonathan Smith": {
        "Adam": {
            "Suzy": {},
            "Clare": {},
            "Aaron": {},
            "Simon": {}
        }, 
        "Timmy": {},
        "Alison": {
            "Natasha": {}, "Zak": {}
        }
     }
}

More powerful structure:

{
    "Smiths": {
        "Jonathan Smith": { "id": 0},
        "Adam Smith": { "id": 1, "father": 0 },
        "Suzy Smith": { "id": 4, "father": 1 },
        "Clare Smith": { "id": 5, "father": 1 },
        "Aaron Smith": { "id": 6, "father": 1 },
        "Simon Smith": { "id": 7, "father": 1 },
        "Timmy Smith": { "id": 2, "father": 0 },
        "Alison Smith": { "id":3, "father": 0 },
        "Natasha Smith": { "id": 8, "father": 3 },
        "Zak Smith": { "id": 9, "father": 3 }
    }
}

Add more relations, mother, husband and wife.

{
    "Smiths": {
        "Jonathan Smith": { "id": 0, "wife": [10]},
        "Suzan Smith": { "id": 10, "born": "Suzan Jones", "husband": [0] },
        "Adam Smith": { "id": 1, "father": 0, "mother": 10 },
        "Suzy Smith": { "id": 4, "father": 1 },
        "Clare Smith": { "id": 5, "father": 1 },
        "Aaron Smith": { "id": 6, "father": 1 },
        "Simon Smith": { "id": 7, "father": 1 },
        "Timmy Smith": { "id": 2, "father": 0, "mother": 10  },
        "Alison Smith": { "id":3, "father": 0, "mother": 10  },
        "Natasha Smith": { "id": 8, "father": 3 },
        "Zak Smith": { "id": 9, "father": 3 }
    }
}

Sometimes it is much easier to work with JSON using Javascript

var familyTree = {}
familyTree["Dick Jones"] = { id: 1234, father: 213 }

This will allow you to add, delete, use functions, be able to check for errors, and then just get the resulting JSON by calling:

JSON.stringify(familyTree)
like image 87
exebook Avatar answered Sep 21 '22 00:09

exebook