Reputation: 31
I am fairly new to neo4j and I want to write a query which reads .json format and create a graph of the nodes. Below is my query which is successful in a file where there is no null property and unsuccessful when I have null property in my json file.
Code:
CALL apoc.load.json("file:/graph-phase1-labelled.json") YIELD value
UNWIND value.nodes as nodes
UNWIND nodes.properties as prop
MERGE(n1:Node{src:prop.sourceIP})
MERGE(n2:Node{dst:prop.destIP})
WITH n1,n2,prop
MERGE (n1)-[:CONNECTED_TO]->(n2)
RETURN n1,n2,prop
Error:
Cannot merge the following node because of null property value for 'src': (:Node {src: null})
Anyone has any idea of what might solve the issue?
Upvotes: 1
Views: 415
Reputation: 31
I have managed to solve the issue by below query:
call apoc.load.json("file:/graph-phase1-labelled1.json") yield value
unwind value.nodes as nodes
unwind nodes.properties as prop
with prop where prop.sourceIP is not null
with prop where prop.destIP is not null
merge(n1:Node{src:prop.sourceIP})
merge(n2:Node{dest:prop.destIP})
with prop,n1,n2
merge (n1)-[ :connected_to]->(n2)
return n1,n2,prop
Upvotes: 0
Reputation: 19373
Since you cannot merge on null properties, you have to filter out the rows which have nulls in them. Example:
CALL apoc.load.json("file:/graph-phase1-labelled.json") YIELD value
UNWIND value.nodes as nodes
UNWIND nodes.properties as prop
WHERE prop.sourceIP IS NOT NULL prop.destIP IS NOT NULL
WITH prop
MERGE(n1:Node{src:prop.sourceIP})
MERGE(n2:Node{dst:prop.destIP})
WITH n1,n2,prop
MERGE (n1)-[:CONNECTED_TO]->(n2)
RETURN n1,n2,prop
Upvotes: 1