Returning formatted json querying a tree with two different relationships

Hi everyone! I have a tree-like structure involving "Blocks", which are connected to other Blocks by "NEXT_BLOCK" or "BLOCK_CHILD". A node can have at most one BLOCK_CHILD and one NEXT_BLOCK, and this can recursively repeat itself.

While I can get all the blocks from the starter node simply by running the following cypher, I would like to get the data in a specific data format.

MATCH (n:Node {id: $nodeId})-[r:NEXT_BLOCK|BLOCK_CHILD*0..]-(b:BLOCK_ELEMENT|BLOCK_INLINE)
RETURN n, r, b

I need to efficiently get this tree of data as an array where each 'Next block' is the next element in the array, while everything that's part of 'BLOCK_CHILD' (including its next nodes) is encapsulated within a children field which is also an array. So the example above would be given as

[
  {
  id : "4fa9125.."
  children : [{text: ""}]
  }, 
  {
  id : "66fb771.."
  children : [
                    {
                      id: "466a6a"
                      children: [{id: "5498a3"}]
                    },
                    {
                      id: "90753..."
                      children: []
                    },
              ]
  }, 
  {
  id : "12258d5.."
  children : [{id: "97b56a"}]
  }, 
]

The organisation can still change if my nodes have a better structural design. These need to be correctly ordered and the order will frequently change though.

I appreciate the help!

Considering this is a hierarchical structure with unbounded depth, it would be challenging to do in cypher. Cypher returns rows, where each row is a path. As such, there is a lot of redundant data and it is not represented as a tree structure. What makes it challenging is cypher is not a scripting language.

This would be fairly easy to implement in a custom procedure, as the hierarchical structure can be created as an algorithm iteratively traverses the graph from the root node.

APOC has a procedure to convert a collections of paths to a tree. I have not used it, but maybe it is applicable.

Hi Gary,

Appreciate the answer!

I'd love to learn more about custom procedures, are they compatible with AuraDB?

I've explored CALL apoc.convert.toTree(paths), and it gets close but unfortunately turns both NEXT_BLOCK relationships and BLOCK_CHILD into children, so I might have to deal with some formatting.

A custom procedure is written using the Neo4j Java API. It is the server code. You interact directly with the entities, not through a query (although you can executed queries too). The code packaged as a jar and deployed to the server's plug-in folder. Unfortunately it is not compatible with Aura, as you have to deploy your custom code to the server.

I have written a library of procedures I wrote to traverse my graphs and calculate metrics. I call them in cypher queries using the CALL method. As an example, th APOC library is a collection of custom procedures and functions deployed as a plug-in.

Writing a Graph Database Stored Procedure in Neo4j – Part I.