Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-07-2021 03:33 AM
Hi ,
I'm new to cypher. I'm trying to import json data with the format
{
"playlists":[{
"tracks":[
{
"track_name":"name",
"artist_name":"artist_name",
"album_name":"album_name"
},
{
"track_name":"name2",
"artist_name":"name2",
"album_name":"name2"
}]
}]
}
im trying to use a cypher like this
UNWIND ['playlist1.json'] as filename
CALL apoc.load.json(filename) YIELD value AS v
WITH v.playlists as mainlist
UNWIND mainlist as playlist
WITH playlist
MERGE (Playlist:Playlist {name: playlist.name})
FOREACH (track in playlist.tracks | MERGE(track:Track{name:track.name})
| MERGE (Playlist)-[:CONTAINS]->(ar:Artist {name: track.artist_name})
| MERGE (Playlist)-[:CONTAINS]->(al:Album{name:album_name)
| MERGE(track.ar-[:RECORDED_FOR]->track.al))
But i'm unable to create the recorded_for relationship due to error. Can someone help me with teh data import ?
04-07-2021 12:48 PM
Try this:
CALL apoc.load.json("file:///playlists.json")
YIELD value as v
WITH v.playlists as mainlist
UNWIND mainlist as playlist
WITH playlist.tracks as t1
unwind t1 as t2
MERGE (Playlist:Playlist {name: "XYZ"})
MERGE (track:Track{name: t2.track_name})
MERGE (ar:Artist {name: t2.artist_name})
MERGE (al:Album {name: t2.album_name})
MERGE (Playlist)-[:CONTAINS]->(ar)
MERGE (Playlist)-[:CONTAINS]->(al)
MERGE (ar)-[:RECORDED_FOR]->(al)
Result:
04-07-2021 01:10 PM
One suggestion regarding the relationships. Here is my suggestion:
MERGE (Playlist)-[:TRACKS]->(track)
MERGE (track)-[:ARTIST]->(ar)
MERGE (track)-[:ALBUM]->(al)
MERGE (ar)-[:RECORDED_FOR]->(al)
All the sessions of the conference are now available online