Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
03-09-2020 01:23 AM
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"
}
]
Order JSON, (the OrderedItems says Pizza and coke are brought together)
[
{
"OrderedItems": [
{
"id": "1",
"name": "Pizza"
},
{
"id": "2",
"name": "Coke"
}
]
},
{
"OrderedItems": [
{
"id": "3",
"name": "Burger"
},
{
"id": "1",
"name": "Pizza"
},
{
"id": "2",
"name": "Coke"
}
]
}
]
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 in advance
Solved! Go to Solution.
03-12-2020 10:29 AM
I tried below
Input
{"items":[{
"OrderedItems": [
{
"id": "1",
"name": "Pizza"
},
{
"id": "2",
"name": "Coke"
}
]
},
{
"OrderedItems": [
{
"id": "3",
"name": "Burger"
},
{
"id": "1",
"name": "Pizza"
},
{
"id": "2",
"name": "Coke"
}
]
}
]
}
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
03-09-2020 01:41 AM
First of all you need to use apoc.load.json to create nodes and relationship between these items .
After that write these logic into your cypher .
please let us know if you require help in writing cypher
03-09-2020 01:45 AM
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?
);
03-10-2020 02:58 AM
Input
{"items":[{
"id":"1",
"OrderedItems": [
{
"id": "1",
"name": "Pizza"
},
{
"id": "2",
"name": "Coke"
}
]
},
{"id":"2",
"OrderedItems": [
{
"id": "3",
"name": "Burger"
},
{
"id": "1",
"name": "Pizza"
},
{
"id": "2",
"name": "Coke"
}
]
}
]
}
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
03-10-2020 10:02 AM
@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" } ] } ]
03-10-2020 11:57 AM
There is a logic given at below link, but it does not fit in your structure completely.
03-11-2020 08:42 AM
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.
Thanks in advance.
03-12-2020 10:29 AM
I tried below
Input
{"items":[{
"OrderedItems": [
{
"id": "1",
"name": "Pizza"
},
{
"id": "2",
"name": "Coke"
}
]
},
{
"OrderedItems": [
{
"id": "3",
"name": "Burger"
},
{
"id": "1",
"name": "Pizza"
},
{
"id": "2",
"name": "Coke"
}
]
}
]
}
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
All the sessions of the conference are now available online