cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

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 :
label/properties:
}]

edges: [
{
source:
target:
}]

Thank you in advance!

2 REPLIES 2

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 -

D_C
Node Clone

@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 result.data() 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.

https://medium.com/neo4j/nxneo4j-networkx-api-for-neo4j-a-new-chapter-9fc65ddab222

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

https://networkx.org/documentation/stable/_modules/networkx/readwrite/json_graph/cytoscape.html

Let me know if you fix it!