Reading nested json from file

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 ?

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:
Screen Shot 2021-04-07 at 12.46.55 PM

1 Like
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)