sharp
sharp

Reputation: 2158

Neo4j - Create Nodes from nested JSON file and list

I am new to Neo4j and especially with Cypher query. I am trying to create nodes for the values in the JSON file as you can see its in a nested format. Using MERGE because I need the values like 'Adam Smith' as a single node from the nested list below. It clearly works well when 'organization' is not in nested/list format. Please see below of my try out and comments. Really appreciate the help!

Sample data.json

[   {   'organization': 'MIT',
        'student_names': ['Adam Smith'],
        'unique_id': 'ABC123'},
    {   'organization': 'Harvard',
        'student_names': ['Adam Smith', 'Cate Scott'],
        'unique_id': 'ABC124'},
    {   'organization': 'Harvard',
        'student_names': ['Mandy T.', 'Bob Smith'],
        'unique_id': 'ABC125'}]

Creates 2 nodes for organization - works

CALL apoc.load.json('file:///data.json') YIELD value as v 
MERGE (o:org {name: v.organization})
// Added 2 labels, created 2 nodes, set 2 properties, completed after 5 ms.

Doesn't work

CALL apoc.load.json('file:///data.json') YIELD value as v 
UNWIND v.student_names as s
MERGE(st:student {name: s.student_names})

Error: Neo.ClientError.Statement.TypeError: Type mismatch: expected a map but was String("Adam Smith")

Looking for a graph untangled like this. I think I can define the relation on my own, I am just looking to see how to create nodes to begin with.

ABC123 --> MIT --> Adam Smith 
ABC124 --> Harvard --> Adam Smith
ABC124 --> Harvard --> Cate Scott
ABC125 --> Harvard --> Mandy T.
ABC125 --> Harvard --> Bob Smith

Upvotes: 0

Views: 1937

Answers (1)

cybersam
cybersam

Reputation: 67019

In your second query, s is already the name of a student, so just do this:

CALL apoc.load.json('file:///data.json') YIELD value
UNWIND value.student_names AS s
MERGE(st:student {name: s})

Upvotes: 2

Related Questions