Dynamically Create the relationship based on condition

I have some records on lavles Employee and its Child labels

Label : Employee | Member| Manager| Lead - An employee can act as either of (Member, Lead, Manager) at the same time for different project Id

Consider now i have a list in java/.net extracted from json . I need to match the type from it and then create a relationship below
{
PersonID : 12345,
PersonName : Test1,
{
"ProjectName": "Project1",
"ProjectId": "1000",
"type": "Lead",
},
{
"ProjectName": "Project2",
"ProjectId": "1001",
"type": "Manager",
},
{
"Project": "Project3",
"ProjectId": "1002",
"type": "Member",
},
}
(person: Employee{PersonID}) - [t:LeadFor]->(ld:Lead {ProjectId})
(person: Employee{PersonID} )- [t:MemberFor]->(m:Manager{ProjectId})
(person: Employee{PersonID} )- [t:ManagerFor]->(m:Member {ProjectId})

I can iterate the list in my code , match the type in the if else statement and create a relationship, but when i have the whole list i want to dynamically through cypher

Hey @Kailash,

If you have installed APOC, then an easy approach is to use apoc.create.relationship().

Here is how I approached it using your JSON file. (Note: I modified it slightly to include a people and projects reference to make it easier to go through the file)

{
  "people":[
  {
    "PersonID": "12345",
    "PersonName": "Test1"
    "projects":[
      {
      "ProjectName": "Project1",
      "ProjectId": "1000",
      "type": "Lead"
      },
      {
      "ProjectName": "Project2",
      "ProjectId": "1001",
      "type": "Manager",
      },
      {
      "Project": "Project3",
      "ProjectId": "1002",
      "type": "Member",
      }
      ]
    }
  ]
}
WITH 'employee-project.json' AS url
CALL apoc.load.json(url) YIELD document
UNWIND document.people AS person
UNWIND document.projects AS project
MERGE (emp:Employee {person_id:person.PersonID})
  ON CREATE
    SET emp.person_name = person.PersonName
MERGE (pro:Project {project_id: project.ProjectID})
  ON CREATE
    SET pro.project_name = project.ProjectName

CALL apoc.create.relationship(emp, project.type, {}, pro) YIELD rel
RETURN rel

Thanks!
This is somewhat i was looking for. but for me i have extracted json object and i already have projects (as above) object with me in my program(which has inner key value), how can i create using apoc.create.relationship or other way?.

So you have the Project nodes (pr) already created. Yes?

And you have the Person nodes (emp) already created. Yes?

So if you can get the type attribute from the JSON object, then just the apoc.create.relationship as I outlined.

CALL apoc.create.relationship(emp, project.type, {}, pr) YIELD rel
RETURN rel

This should work as long as you have extracted the type from the Project object, and can use it in the apoc.create.relationship() function.

Yes, I have both project node created already, and also the employee node will also get created before i execute this query on top. I have both the nodes already. now based on the fields inside the list (lead, member, manager), i need to create that relationship with the matching (emp:Employee) --[Manager/Member/Lead]-(pro:Project)

So this will work if I understand you correctly.

Yessir! That is exactly correct.

-yyyguy

1 Like