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.

How to create relation from two json files and link them?

I have two json files. I want create a relationship between them. Have a look on my json file:

First Json file:

{
"id": "1234"
}

Second Json File:

{"abstract":
 [
		{

			"value":   "<p>chinese</p>",

			"language": "chi"
		},

		{
			"value": "<p>eng</p>",
			
			"language": "eng"
		}
],		
"id":"1234"
}

I need to link two json files based on the id. The problem here is in second json file when I create node there will be two nodes since its an array. so I want link the two node in the second json file to single node on the first json based in the id.

This is code i have used to create node for second json file:

CALL apoc.load.json("file:///D:/secondjson.json") YIELD value as data
FOREACH (abst IN data.abstract | MERGE (abs:Abstract{value:abst.value}) ON CREATE SET abs.language=abst.language

Any hints or suggestions will be great help.

Thank you

1 ACCEPTED SOLUTION

Thank you very much michael for your approach. It worked for me. But in my case below code worked for me

CALL apoc.load.json("file:///D:/second.json") YIELD value as data
MERGE (a:Paper {id:data.id})
FOREACH (abst IN data.abstract | 
MERGE (a)-[:CONTAINS]->(abs:Abstract{value:abst.value}) 
ON CREATE SET abs.language=abst.language
)

View solution in original post

2 REPLIES 2

I would use this approach:

CALL apoc.load.json("file:///D:/secondjson.json") YIELD value as data
MATCH (n:Paper {id:data.id})
FOREACH (abst IN data.abstract | 
  MERGE (n)-[:CONTAINS]->(abs:Abstract{value:abst.value}) 
  ON CREATE SET abs.language=abst.language
)

which creates/merges the Abstracts in the context of the main node.
so if there is no abstract yet with that value connected to that Paper node it will create the relationship and the Abstract node

Thank you very much michael for your approach. It worked for me. But in my case below code worked for me

CALL apoc.load.json("file:///D:/second.json") YIELD value as data
MERGE (a:Paper {id:data.id})
FOREACH (abst IN data.abstract | 
MERGE (a)-[:CONTAINS]->(abs:Abstract{value:abst.value}) 
ON CREATE SET abs.language=abst.language
)