Neo4j return nested JSON

Viewed 4494

My Neo4j database contains family tree relationships.

I would like to extract that data in a nested JSON format like so:

{  
"firstname":"Jon",
"lastname":"Smith",
"parents":[  
  {  
     "firstname":"Anna",
     "lastname":"Smith",
     "parents":[  
        {  
           "furstname":"Peter",
           "lastname":"Doe",
           "parents":[  
              {  
                 "firstname":"Jessica",
                 "lastname":"Doe"
              },
              {  
                 "firstname":"Clayton",
                 "lastname":"Doe"
              }
           ]
        },
        {  
           "firstname":"Nell",
           "lastname":"Gordon",
           "parents":[  
              {  
                 "firstname":"Jessica",
                 "lastname":"Roberts"
              },
              {  
                 "firstname":"Randy",
                 "lastname":"Roberts"
              }
           ]
        }
     ]
  }
]
}

in order to visualize it.

I have tried the following query:

MATCH path = (p:Person)-[r:PARENT_OF*1..3]-(k:Person) 
WHERE k.id = '1887' 
UNWIND r as rel 
RETURN StartNode(rel).firstname, rels(path), EndNode(rel).firstname

with the py2neo library like so:

dumps(graph.run(query).data())

but the JSON was not nested like I desired.

Is there a query that would help me achieve this or I should do the nesting in other programming language?

1 Answers
Related