Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-23-2019 05:41 AM
Hello,
I have 2 different JSON files but with a common field in the 2 files. I would like to create a node with properties coming from the 2 files.
how can i do this please?
F1.json:
{
[
{
"id" : "AAA",
"name" : "text",
"capacity" : 1234
},
{...},
{...}
]
}
F2.json:
{
[
{
"id" : "AAA",
"status" : "text"
},
{...},
{...}
]
}
i would like to create a node like this (:node {id:"AAA","name" : "text", "capacity" : 1234, "status" : "text"})
Thnaks for you help.
Solved! Go to Solution.
12-23-2019 09:03 AM
First of all, these files are not valid json files. If you want to include a list of nodes, define a key of type list like this:
F1.json:
{
"nodes": [
{
"id" : "AAA",
"name" : "text",
"capacity" : 1234
},
{...},
{...}
]
}
Now assuming that you've done that, there are different ways to do what you described but this is the easiest:
CALL apoc.load.json("F1.json") YIELD value
UNWIND value.nodes AS node
WITH node
MERGE (u:User {id: node.id})
SET u.name=node.name, u.capacity=node.capacity
WITH "1" as t
CALL apoc.load.json("F2.json") YIELD value
UNWIND value.nodes AS node
WITH node
MERGE (u:User {id: node.id})
SET u.status=node.status
WITH
before loading the second json file and that's why I added that arbitrary WITH "1" as t
.MERGE
has the ON CREATE
and ON MATCH
options to set properties only when a new node gets created or it is just matched with an existing node. So feel free to use them to make your cypher more efficient. For example, if you know that name
and capacity
of a user do not change, then you can replace the first MERGE
with this:MERGE (u:User {id: node.id})
ON CREATE SET u.name=node.name, u.capacity=node.capacity
or if you know that no new node gets created by the second json file, you can replace the second MERGE
with this:
MATCH (u:User {id: node.id})
SET u.status=node.status
12-23-2019 09:03 AM
First of all, these files are not valid json files. If you want to include a list of nodes, define a key of type list like this:
F1.json:
{
"nodes": [
{
"id" : "AAA",
"name" : "text",
"capacity" : 1234
},
{...},
{...}
]
}
Now assuming that you've done that, there are different ways to do what you described but this is the easiest:
CALL apoc.load.json("F1.json") YIELD value
UNWIND value.nodes AS node
WITH node
MERGE (u:User {id: node.id})
SET u.name=node.name, u.capacity=node.capacity
WITH "1" as t
CALL apoc.load.json("F2.json") YIELD value
UNWIND value.nodes AS node
WITH node
MERGE (u:User {id: node.id})
SET u.status=node.status
WITH
before loading the second json file and that's why I added that arbitrary WITH "1" as t
.MERGE
has the ON CREATE
and ON MATCH
options to set properties only when a new node gets created or it is just matched with an existing node. So feel free to use them to make your cypher more efficient. For example, if you know that name
and capacity
of a user do not change, then you can replace the first MERGE
with this:MERGE (u:User {id: node.id})
ON CREATE SET u.name=node.name, u.capacity=node.capacity
or if you know that no new node gets created by the second json file, you can replace the second MERGE
with this:
MATCH (u:User {id: node.id})
SET u.status=node.status
12-24-2019 02:51 AM
Thank you so much Sayed Hossein. it's so kind of you to find a solution to my question.
I will try your suggestion and will say you if that works.
by the way, i'm persian too and living in Paris (Damet garm) 🙂
can i contact you when i have others questions about neo4j ? I have been learning this database for 2 weeks.
01-09-2020 07:56 AM
Oh just saw your message. Sure I'll be happy to help, and (Dame Khodet Ham Garm )
All the sessions of the conference are now available online