(Python, Neo4j) - How to using UNWIND to create nodes and relations by get value from dictionary?

Are there anyhow to create node by get data from dictionary below:

node = {'mike':22, 'jack':19, 'alex':24}
query = """
UNWIND {{batch}} as row
CREATE (n:SINGLE_NODE)
SET n += row
""".format(node =node )
session.run(query, node =node )

i want key for name and number for age properties.

And For relationship:

relations = {'mike|jack':1,'mike|jack':2,'jack|alex':3}
query = """
    UNWIND {{relations}} as row
    MATCH (from:SINGLE_NODE {{name:row.from}})
    MATCH (to:SINGLE_NODE {{name:row.to}})
    MERGE (from)-[rel:IS_CONNECTED]->(to)
    ON CREATE SET rel += row.count
    """.format(relations=relations)
session.run(query, relations =relations )

i want to split key for match node in database (ex. 'mike|jack' -> row.from = 'mike' , row.to = 'jake') and value of key for set relationship properties.

The data in dictionary variable come from by getting word in text document and i want to avoid using list variable ( [{ },{ },...] ) because it waste too much time to keep word from text file.

In both cases you need to be passing in an array and not a single object. For example in the relations example you should change this:

relations = {'mike|jack':1,'mike|jack':2,'jack|alex':3}

To this:

relations = [ { 'from': "mike", 'to': "jack", 'age': 1 }, { 'from': "mike", 'to': "jack", 'age': 2 }]

The UNWIND in your cypher has to operate over an array. Once you unwind the array to an individual object, it's going to be far easier to refer to object.from instead of trying to parse "mike|jack"

In your first example, it's the same thing again - to use UNWIND you need an array, not a single item. UNWIND does not take apart an object key by key

1 Like