title: Hierarchical Cypher query with literal result
body: <p>My object structure looks like this: there are Containers as a root nodes, which have subcontainers. Each subcontainer has tasks, which also have subtasks. To illustracte this in Cypher MATCH: </p>
  
  <blockquote>
    <p>(container)-[:HAS_SUBCONTAINER]->(subcont)-[:HAS_TASK]->(task)-[:HAS_TASK]->(subtask)</p>
  </blockquote>
  
  <p>I want to write a query which returns JSON literal representation for this hierachy:</p>
  
  <pre><code>{
    name: 'Main',
    subcontainers: [
      {
        name: 'Subcont',
        tasks: [
          {
            name: 'parent1',
            children: [
              {
                name: 'child1'
              }
            ]
          }
        ]
      }
    ]
  }
  </code></pre>
  
  <p>Is there a way to do this in Cypher with one query? I found solution for one level hierarchy:</p>
  
  <pre><code>MATCH (cnt:Container {name: 'Main'})-[:HAS_SUBCONTAINER]-&gt;(subcnt)
  RETURN { name: cnt.name, subcontainers: EXTRACT(subc IN collect(subcnt)|{name: subc.name})}
  </code></pre>
  
  <p>But can't fugure out how to do it for more complex case. Any ideas?</p>