cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to loop over an object in neo4j using cypher

Hi i am using nodejs with neo4j-driver and i create relations like this (USER-[ values from the object ]->SUBREDDIT)

Username is predefined so this isn't a problem but the subreddit name and values of relations is inside the object.

The object is really huge so i would prefer use some cypher magic to loop over this object and create relations, bad i have no idea how to do it.

I am also wondering if this is good approach to do that or if it is more efficient to just loop over this object in JavaScript and make more queries

'62': {
  n: 1,
  ups: 15.730142943532986,
  gilded: 0,
  upvote_ratio: 0.9253025260901756,
  num_comments: 2.7759075782705267,
  num_crossposts: 0,
  score: 15.730142943532986,
  sub: 'r/aww'
},
'63': {
  n: 2,
  ups: 33.58852601401853,
  gilded: 0,
  upvote_ratio: 1.2799872062537179,
  num_comments: 7.8618886115333115,
  num_crossposts: 0,
  score: 33.58852601401853,
  sub: 'r/FreeKarma4U'
}
1 ACCEPTED SOLUTION

Since you didn't provide any real details about how "username is predefined", I can only help you get kinda there.

apoc.load.json.

CALL apoc.load.json('yourBigJsonBlob.file-or-uri') YIELD value
UNWIND [k IN KEYS(value) | value[k]] AS obj
MERGE (s:Subreddit {name: obj.sub})

// ... find the user, then...

MERGE (user)-[r:SUBS_TO]->(s)
SET r = obj

View solution in original post

1 REPLY 1

Since you didn't provide any real details about how "username is predefined", I can only help you get kinda there.

apoc.load.json.

CALL apoc.load.json('yourBigJsonBlob.file-or-uri') YIELD value
UNWIND [k IN KEYS(value) | value[k]] AS obj
MERGE (s:Subreddit {name: obj.sub})

// ... find the user, then...

MERGE (user)-[r:SUBS_TO]->(s)
SET r = obj