cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Import csv file (dictionary format?)

Novice
Node Link

Hi, I use neo4j browser and i have a Q.
Can I import a csv file like this format? (no column data on top)

LOAD CSV FROM '{csv-dir}/neo4j-sample-data.csv' AS info FIELDTERMINATOR ','
CREATE (:Info { name: info[1].name, count:info[1].count. asset_id:info[1].asset_id})


I want get Datas { name, count, asset_id} .
Is it possible?Thank you.

6 REPLIES 6

sam_gijare
Graph Buddy

Hello
Please check cypher query syntax for exact details on cypher syntax reference card.
Thanks
SameerG

Hey,
Thanks for reply and I did it.
read syntax reference. but, failed

It is so difficult to me
oh my.....

Hello
Please read the error carefully. After careful consideration it looks like problem is with
name: info[1].name column it is expecting a map data structure but you are supplying a string instead of Map. So please look for conversion functions from string to map on ref card that I earlier pointed to.
I hope that this will fix the issue at your end.
Sameer

anthapu
Graph Fellow

It seems you have a json object as the value in CSV. This is not how load csv works. It converts a single row into map.

For ex:

if you have data

id, name
1,test
2,test2

the resulting map would be

{
   id: 1,
   name: "test"
}

In your case the map seen by db would be

{
    n: "{name:CVE-2014-3539,count:1,asset_id:26...}"
}

All the values if the type cannot be inferred are treated as strings.

So "info.n" will give you access to the string. But you need convert that string to map. Even with apoc it might not be possible because this is not a valid json, as the strings and names are enclosed in quotes.

If you want this data in CSV format then it should be like this

name,count,asset_id,privilege,seq
CVE-2014-3539,1,21635,0,1

clem
Graph Steward

What you might want to try, is import as CSV, then take the string (which you want to become a JSON), and apply a String -> JSON function.

See:

Hi @Novice ,

this is my code, please give a try

demo.csv

n
{name:CVE-2014,count:1,asset_id:1111,privilege:0,seq:1}
{name:CVE-2015,count:12,asset_id:2222,privilege:0,seq:2}
{name:CVE-2016,count:31,asset_id:3333,privilege:0,seq:3}

Cypher Query

CALL apoc.load.csv('demo.csv' ,{skip:1,header:FALSE} )
YIELD   list
with  apoc.convert.toString(list) as base
WITH ['\{', '\}','\[','\]',  'name:','count:','asset_id:','privilege:','seq:', '\"'] as to_remove, base
with base, reduce(vari=base, word in to_remove | apoc.text.replace(vari,word,'')) as filtered 
with  apoc.convert.toStringList(split(filtered,",")) as tt
create (a:Item) set a.name = trim(tt[0]), a.count=toInteger(trim(tt[1])),a.asset_id=toInteger(trim(tt[2])),a.privilege=trim(tt[3]),a.seq=toInteger(trim(tt[4]))

Result -

match(n) return(n)
╒══════════════════════════════════════════════════════════════════════╕
│"n"                                                                   │
╞══════════════════════════════════════════════════════════════════════╡
│{"name":"CVE-2014","count":1,"privilege":"0","asset_id":1111,"seq":1} │
├──────────────────────────────────────────────────────────────────────┤
│{"name":"CVE-2015","count":12,"privilege":"0","asset_id":2222,"seq":2}│
├──────────────────────────────────────────────────────────────────────┤
│{"name":"CVE-2016","count":31,"privilege":"0","asset_id":3333,"seq":3}│
└──────────────────────────────────────────────────────────────────────┘

Note -

  1. above script has CREATE not MERGE. So, be careful with incremental inserts. make sure the csv files doesn't have duplicates. (you can create constraint, but didn't try it).
  2. I didn't know about the data, so have few data converted to int.
  3. i doubt your csv file. was it originally a json file and then you removed "," at the end ? I am not sure.

give it a try and let me know how it goes.