I need to import data from a csv of the form
id;name;targetset
1;"somenode",[1,3,5,8]
2,"someothernode",[3,8]
into the graph and I need to have targetset stored as 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 stores targetset as a string, e.g. "[1,3,5,8]". There does not seem to be a function to convert array-encoding-strings to actual arrays, like there is toInt to convert strings to integers. Is there still another possibility?
APOC Procedures will be your best bet here. Use the function apoc.convert.fromJsonList().
An example of use:
WITH "[1,3,5,8]" as arr
RETURN apoc.convert.fromJsonList(arr)
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 remove the [ and ] chars from the string [1,3,5,8] using substring() and length() functions. After the string 1,3,5,8is splited considering , as separator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With