Cypher convert to JSON of nodes and edges

Hi guys i'm trying to export to JSON dynamically based on Cypher.

MATCH (c:Claim)-[]->(a) WHERE c.claimID="6000" RETURN c, a

But I want to convert this to JSON of

nodes : [
id :

edges: [

Thank you in advance!

1 Like

You'll need to get an alias on the relationships/edges if you want to include them in your json output, one approach to this is to construct a subgraph (copied from the linked post) -

MATCH (n) 
WHERE n.user_id='0000001'
CALL apoc.path.subgraphAll(n, {maxLevel:1}) YIELD nodes, relationships
WITH [node in nodes | node {.*, label:labels(node)[0]}] as nodes, 
     [rel in relationships | rel {.*, fromNode:{label:labels(startNode(rel))[0], key:startNode(rel).key}, toNode:{label:labels(endNode(rel))[0], key:endNode(rel).key}}] as rels
WITH {nodes:nodes, relationships:rels} as json
RETURN apoc.convert.toJson(json)

Original post & explanation -

@afiqqqx1997 did you get this to work? It looks like your format is similar to what cytoscape.js will use, which is exactly what I'm about to try and do.

I guess you know that will give you a dict, which you could go through and pull out what you want on per path basis? At least then you have an object to work with, not raw JSON.

But it seems there isn't a simple conversion to get the structure you outlined.

One thing I'm considering is going via networkX graph library which has a driver for neo4j.

and networkX will give cytoscapeJS format. Another option is to use the nx code directly for this conversion and make your own library:

Let me know if you fix it!