3
votes

I'd like to make a cypher query that generates a specific json output. Part of this output includes an object with a dynamic amount of keys relative to the children of a parent node:

{
   ...

   "parent_keystring"                 : {
           child_node_one.name        : child_node_one.foo
           child_node_two.name        : child_node_two.foo       
           child_node_three.name      : child_node_three.foo
           child_node_four.name       : child_node_four.foo
           child_node_five.name       : child_node_five.foo
   }
}

I've tried to create a cypher query but I do not believe I am close to achieving the desired output mentioned above:

MATCH (n)-[relone:SPECIFIC_RELATIONSHIP]->(child_node)
WHERE n.id='839930493049039430'
RETURN n.id          AS id,
       n.name        AS name,
       labels(n)[0]  AS type,
       {
           COLLECT({ 
              child.name : children.foo
           }) AS rel_two_representation
       } AS parent_keystring

I had planned for children.foo to be a count of how many occurrences of each particular relationship/child of the parent. Is there a way to make use of the reduce function? Where a report would generate based on analyzing the array proposed below? ie report would be a json object where each key is a distinct RELATIONSHIP and the property value would be the amount of times that relationship stems from the parent node?

Thank you greatly in advance for guidance you can offer.

2
dynamic keys don't work yet, I think there might be some plans to add them in the futureMichael Hunger
Since then, user-defined procedures (APOC) support this, see this answer: stackoverflow.com/a/41907779/3580502Gabor Szarnyas

2 Answers

1
votes

I'm not sure that Cypher will let you use a variable to determine an object's key. Would using an Array work for you?

COLLECT([child.name, children.foo]) AS rel_two_representation
0
votes

I think, Neo4j Server API output by itself should be considered as any database output (like MySQL). Even if it is possible to achieve, with default functionality, desired output - it is not natural way for database.

Probably you should look into creating your own server plugin. This allows you to implement any custom logic, with desired output.