Search code examples
javascriptjsonneo4jcypherneo4j-apoc

How to get complete graph as JSON from NEO4J in JavaScript


I see apoc.export.json.data but don't see complete working code example..

I imagine the MATCH query would enable you to get any graph:

MATCH g=(someNode)-[someRel]-() return g
CALL apoc.export.json.data( g )

Then hopefully the APOC would return JSON of all the nodes and edges in the dataset resulting from the query. Expected JSON:

{
  nodes:[
    { id:a1a1 , labels:[Something] , prop_a:99 },
    { id:a2a2 , labels:[Something] , prop_a:77 },
    { id:a3a3 , labels:[User] ,      prop_a:33 }
  ],
  edges:[
    { id:a1a1 , labels:[OWNS] , prop_a:99 },
    { id:a2a3 , labels:[OWNS] , prop_a:77 },
    { id:a4a5 , labels:[HAS] ,  prop_a:33 }
  ]
}


Solution

  • Instead of using an APOC, you can create your own output like below;

    MATCH   g = ()-[]-()  
    RETURN {nodes: apoc.coll.flatten(collect(distinct nodes(g))), 
            edges: apoc.coll.flatten(collect(distinct relationships(g))) } as output
    

    I used Enterprise edition ver 5.4 so it should work as well.

    Sample result:

    enter image description here