Hi all,
I have a json file containing array of objects
Sample records from file is as below:
Item JSON,
[
{
"id": "1",
"name": "Pizza"
},
{
"id": "2",
"name": "Coke"
},
{
"id": "3",
"name": "Burger"
}
]
I need to create relationships between these objects can someone help me out,
My logic follows:
I will pass an item from the item object to the order JSON object it iterates through the all the ordered items in the object gives result as below,
If I pass Pizza, it gives output as Pizza ->(Ordered_with)-> Coke (Here two times pizza ordered with coke)
If I pass Burger, it gives output as Burger ->(Ordered_with)-> Coke
If I pass Coke, it gives output as Coke ->(Ordered_with)-> Pizza, Burger.
Thanks much for your reply @12kunal34
I have used apoc.load.json to create nodes
// import item_details
WITH "file:///item_details.json" AS url
CALL apoc.load.json(url) YIELD value as item
MERGE (i:Item {item_id: item.id})
SET i.name = item.name
But how to create relationship between ordered items while importing,
I am struggling a bit could you please help me out?
Below is the half written query,
// import line_item_details
WITH "file:///orders.json" AS url
CALL apoc.load.json(url) YIELD value
WITH value.OrderedItems AS OrderedItems
FOREACH (OrderedItem IN OrderedItems |
MERGE (o:OrderedItems {item_id: OrderedItem.id}) // How to create relationship here?
);
Ingestion Code
WITH "file:///ref.json" AS url
CALL apoc.load.json(url) YIELD value as item
unwind item.items as aoid
Merge (oid:OrderItemID{id:aoid.id})
Foreach(a in aoid.OrderedItems| Merge(item:Item{id:a.id,name:a.name}) Merge(oid)-[:ORDERED]->(item))
Query Code
match path=(a:OrderItemID)-->(b:Item) With a, collect(b) as Item Together return a.id,Item Together
@intouch_vivek Thanks much for the answer.
But in my actual data structure, there won't be any orderItemID, it will just contain orderItems in array of objects.
Is there any other way which adds relationships between orderItems without creating additional orderItemID nodes?
Below is data structure, [ { "OrderedItems": [ { "id": "1", "name": "Pizza" }, { "id": "2", "name": "Coke" } ] }, { "OrderedItems": [ { "id": "3", "name": "Burger" }, { "id": "1", "name": "Pizza" }, { "id": "2", "name": "Coke" } ] } ]
Thanks much for suggesting.
The link above describes to create relationship between list of nodes not between the elements within a list.
It would be great, if you could able to solve this.
I have tried my level best to solve this, but am really struggling a way to create relationship between the elements within a list.
Ingestion
WITH "file:///kishore" AS url
CALL apoc.load.json(url) YIELD value as item
unwind item.items as aoid
Create (oid:OrderItemID)
Foreach(a in aoid.OrderedItems| Merge(item:Item{id:a.id,name:a.name}) Merge(oid)-[:ORDERED]->(item))
return aoid
Create link between Items
match(n:OrderItemID)-[:ORDERED]->(m:Item) with n,collect(m) as a CALL apoc.nodes.link(a, 'TOGETHER')
RETURN n,a