Написал код, но он не так как надо работает...
$(document).ready(function(){
var foo =[{"id":"1","name":"Киевский","surname":"Дмитрий","id_fat":"3","id_mot":"2"},{"id":"2","name":"Ращупкина","surname":"Елизавета","id_fat":"7","id_mot":"0"},{"id":"3","name":"Киевский","surname":"Владимир","id_fat":"6","id_mot":"5"},{"id":"4","name":"Третьяков","surname":"Антон","id_fat":"3","id_mot":"2"},{"id":"5","name":"","surname":"Галина","id_fat":"0","id_mot":"0"},{"id":"6","name":"Киевский","surname":"Дмитрий","id_fat":"0","id_mot":"0"},{"id":"7","name":"Ращупкин","surname":"Виталий","id_fat":"0","id_mot":"0"}];
//console.log(foo[0].nom)
string = ""
for(i=0;i<foo.length;i++){
namef = '';
namem = '';
if(foo[i].id_fat != 0 || foo[i].id_mot != 0){
idf = foo[i].id_fat;
idm = foo[i].id_mot;
for(j=0;j<foo.length;j++){
if(foo[j].id == idf){
namef = foo[j].name+' '+foo[j].surname
}
if(foo[j].id == idm){
namem = foo[j].name+' '+foo[j].surname
console.log(namem)
}
}
console.log(foo[i].id+' '+foo[i].name+' '+foo[i].surname+' '+namef+' '+namem)
string = string + '{"name":"'+foo[i].name+' '+foo[i].surname+'","children": [{"name": "'+namef+'"}, {"name": "'+namem+'"}],}'
}
}
console.log(string)
})