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).
' { } ' used for Object and ' [] ' is used for Array in json.
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.
JSON has three types of nodes, which are Value, Object and Array. We know that JSON nodes have a hierarchical tree structure.
Valid Data Typesan object (JSON object) an array. a boolean. null.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With