Neo4j cypher storage array property during csv import
I need to import data from a csv form
id;name;targetset
1;"somenode",[1,3,5,8]
2,"someothernode",[3,8]
in a graph, and I should have targetset
stored as a collection (array) using cypher. I tried
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:/mytable.csv" AS row FIELDTERMINATOR ';'
CREATE (:MyNode {id: row.id, name: row.name, targetset: row.targetset});
but it saves targetset
as a string eg. "[1,3,5,8]"
... There seems to be no function to convert array-array strings to real arrays, such toInt
as converting strings to integers. Is there another possibility?
source to share
APOC procedures would be best here. Use a function apoc.convert.fromJsonList()
.
Usage example:
WITH "[1,3,5,8]" as arr
RETURN apoc.convert.fromJsonList(arr)
source to share
You can try this:
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:/mytable.csv" AS row FIELDTERMINATOR ';'
CREATE (:MyNode {id: row.id, name: row.name, targetset: split(substring(row.targetset, 1, length(row.targetset) - 2), ',') });
The above code removes characters [
and ]
from a string [1,3,5,8]
using substring () and length () . After the string is 1,3,5,8
split with account ,
as delimiter.
source to share