title: Cypher Query Language/Neo4j - Nested Returns of Variable Path Length
body: <p>I have a graph structure in Neo4j for a questionnaire that has the following relationships:</p>
  
  <pre><code>    (a:Category)-[:INITIAL_QUESTION]-&gt;(b:Question)-[c:Answer*]-&gt;(d:Question)
  </code></pre>
  
  <p>where the specific question text is contained in <code>(b|d).text</code> and the possible answers for each question is contained in the relationship <code>c.response</code>
  From the initial question, there are some paths that are longer than others. I would like the return to look something like this:</p>
  
  <pre><code>{"category": "example questionnaire category",
  "initialQuestion" : {
                      "id": 1,
                      "text": "Example text here",
                      "possibleAns": {
                        "yes" : {
                                  "id": 2,
                                  "text": "Second question text here?",
                                  "possibleAns": {
                                                    "ans1": {/*Question 4 containing all possible child question nodes nested
                                                                in possibleAns*/},
                                                    "ans2": {/*Question 5 containing all possible child question nodes nested
                                                                in possibleAns*/},
                                  }
                        },
                        "no" :{
                                  "id": 3,
                                  "text": "Different question text here?",
                                  "possibleAns": {
                                                    "ans1": {/*Question 6 containing all possible child question nodes nested
                                                                in possibleAns*/},
                                                    "ans2": {/*Question 7 containing all possible child question nodes nested
                                                                in possibleAns*/},
                                  }
                        }
                      }
   }
  }
  </code></pre>
  
  <p>so that the entire category questionnaire is contained in a single, nested map. I've seen some other examples, but haven't been able to tweak those queries to fit my needs, especially given the variable depth of the questionnaire branches.</p>
  
  <p>Is there a Cypher query that makes this possible? If not, what would be the best approach for retrieving the entire questionnaire from the db? </p>