How to create a family tree structure in JSON format

I am experimenting with the JSON format and am not sure how to use it to structure the family tree. This is what I have (to make it simple, I listed only the father, his children and whether these children had any children. I did not list the names of the spouses).

{ "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 checks the fine, I'm not sure what the best way to do this (i.e. my DRY approach and is scalable, for example).

+6
source share
4 answers

The easiest 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 relationships, 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 with Javascript.

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

This will allow you to add, remove, use functions, check for errors, and then just get the received JSON by calling:

 JSON.stringify(familyTree) 
+4
source

you should pay attention since in json format you are adding an override. Try using a structure that allows you to answer a request that you want to make in a simple way.

0
source

Try the following:

 {'name': 'John'}, {'name': 'Jack', 'child_of': 'John'}, {'name': 'Charlie', 'child_of': 'Jack', 'grand_child_of': 'John'} 
0
source

Working with trees can be complicated in JSON, but perhaps you can use the concept of levels (generations in this example) so that you can learn about uncles, cousins, etc.

  [ { "id":100, "name":"Jhon Smith", "generation":1, "children":[ { "id":101, "name":"Michael Smith", "generation":2, "children":null }, { "id":102, "name":"Diana Smith", "children":[ { "id":301, "name":"Britney Smith", "generation":3, "children":null } ] } ] }, { "id":200, "name":"Richard Smith", "generation":1, "children":[ { "id":101, "name":"Michael Smith", "generation":2, "children":null }, { "id":102, "name":"Diana Smith", "generation":2, "children":null } ] } ] 
0
source

Source: https://habr.com/ru/post/957248/


All Articles