Load csv where header is represented as the key

It's a sort of regular problem but I still cannot figure out the solution if not to use any plugin-in libraries.

Here is code snippet as follows, and I just iterate over rows and project each cell onto the corresponding header.

load csv with headers from "file:///data.csv" as row
CREATE (n:Test)
WITH row, n
UNWIND keys(row) AS k
with row, k, n
SET n += { k: row[k]}

The question is how to set a variable k as key within properties of node.
Unfortunately, I always get literally key as "k" and sequentially overwrite the previous result from the SET clause.

Thanks for guys time for reading.

Hello @silenceliang and welcome to the Neo4j community :slight_smile:

It is not possible to create a dictionary dynamically only in Cypher. You will have to use the function apoc.load.csv() in the APOC plugin. This function allows you get each row as a map.

CALL apoc.load.csv('data.csv')
YIELD lineNo, map, list
CREATE (n:Test)
SET n += map


1 Like

Hi Cobra,

Thanks for the quick reply!
I was expected to implement such a handy function by naive cypher syntax before.

Appreciate a lot I will try to use APOC plugins and go on exploring neo4j !


1 Like