Ahmet Tanakol
Ahmet Tanakol

Reputation: 899

Dynamically create property names from key-value pairs loaded from CSV

I am trying to load data from a csv file in Neo4j.In my file, I have key and value columns. I need to set the name of the property of the node by using the key and then I want to set the value of this key. As an example,

Key   | Value
______________
Name | John
Age  | 23

The property on the node should look like Name: John, Age: 23. My SET command is wrong but I couldn't figure out how to fix it. If you can help me, it would be great.

LOAD CSV WITH HEADERS FROM 'file:///properties.csv' AS line
MATCH (n {id:line.ID})
SET n[line.KEY] = line.Value

Thanks

Upvotes: 0

Views: 1412

Answers (1)

rickhg12hs
rickhg12hs

Reputation: 11942

As described in Mark Needham's blog post, you can dynamically set property names using apoc.create.setProperty().

So, rather than

SET n[line.KEY] = line.Value

use

CALL apoc.create.setProperty(n, line.KEY, line.Value)

Upvotes: 0

Related Questions