1
votes

I need to read a json file having this structure:

{
    "source":"source name",
    "target":"target name",
    "targettype":"targettype",
}

The nodes source and target are already loaded in my graph. And they have distinct labels. Target names are not unique but each duplicate would be in relationship with a different targettype

There are then two distinct groups of target node, those that have a type and those that do not have a type; in the graph those nodes have distinct labels please note that when a node has no type, the json value for targetype is null

The json file is meant to load the relationship between the source and the target using the name

I am pretty lost on how to achieve it. As I need a IF THEN ELSE I TRIED TO USE two FOREACH (ignoreme ) followed by the MATCH but this is not allowed .....

The pseudo code would be

LOAD lines from json
WITH blah blah blah
UNWIND blah blah blah
IF targettype is NULL THEN
  MATCH (n:source_name)--(sourcetype)
  MATCH (m:NULL_TARGET_LABEL target_name)
ELSE
  MATCH (n: source_name)--(sourcetype)
  MATCH (m:NON_NULL_TARGET_LABEL target_name)--(targettype)
END
MERGE (n)-[:TIGHT_IT_TO]_(m)
RETURN *
1
Where does sourcetype come from? what does (n:source_name)--(sourcetype) mean?NonameCurious
I start with loading data from json so source_name and sourcetype contain the values loaded from the json MATCH(n: {name:source_name)}--(sct: {name:sourcetype})Bruno C

1 Answers

3
votes

The APOC procedure apoc.do.when should be helpful. For example:

...
MATCH (n:source_name)--(sourcetype)
CALL apoc.do.when(
  targettype is NULL,
  "MATCH (m:NULL_TARGET_LABEL {name: target_name}) RETURN COLLECT(m) AS ms",
  'MATCH (m:NON_NULL_TARGET_LABEL {name:target_name})--(targettype) RETURN COLLECT(m) AS ms',
  {target_name:target_name, targettype:targettype}) YIELD value
UNWIND value.ms AS m
MERGE (n)-[:TIGHT_IT_TO]-(m)
...