Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-22-2022 11:04 AM
I have a csv file with the following headers
node_label, node_id, node_properties
The headers node_label and node_id are both strings. node_properties is a string representation of a json containing a dynamic number of properties for the node.
How can I load these nodes in with these dynamic properties?
Solved! Go to Solution.
12-22-2022 02:02 PM
You got it, except I made a mistake on the apoc method. The method I gave you converts a json string to a map, when the json string is stored in a node's property. You want to use 'apoc.convert.fromJsonMap' instead .
try this:
// Go over each row in the csv
load csv with headers from "file:///sample.json" AS row
// Initialize the node
CALL apoc.create.node([row.node_label], {})
// Grab a reference to the node we just created (not sure if this is valid)
YIELD node
// Set the node equal to what you mentioned above
set node = apoc.convert.fromJsonMap(row.node_properties)
// Set the id here since the id is not defined in row.node_properties
set node.id = row.node_id
RETURN count(*)
12-22-2022 11:10 AM
I just realized that I posted this question to "General Discussions", which is not the appropriate place to ask this kind of question. Apologies for that, but with that said I don't see an option to delete my post. So with that said, if anyone has any insight into my question, I would be very appreciative.
12-22-2022 11:33 AM
Does the string represent a json object of just key/value pairs and the values are valid neo4j property types? If so, you can use apoc.convert.getJsonPropertyMap to convert the json to a map. Next, set the node variable equal to the map. That will replace all properties with the key/value pairs in the map. You then need to set the id, as it would have been removed with the first ‘set’ operation, if you placed it in the node to match on.
https://neo4j.com/labs/apoc/4.1/overview/apoc.convert/apoc.convert.getJsonPropertyMap/
If the string represents a json objects whose keys can be another json object, then you will need to extract the key/value pairs separately and set the properties individually.
I can assist with a query if you need help.
12-22-2022 01:36 PM
Thank you very much for the information! To answer your question, the string does consist of key/value pairs where the values are valid neo4j property types. I am currently trying to incorporate the information you provided into a query, but that has yet to work. This is what I currently have with comments to show my thinking for each line
// Go over each row in the csv
load csv with headers from "file:///nodes_and_prop_map.csv" AS row
// Initialize the node
CALL apoc.create.node([row.node_label], {})
// Grab a reference to the node we just created (not sure if this is valid)
YIELD node
// Set the node equal to what you mentioned above
node = apoc.convert.getJsonPropertyMap(row.node_properties)
// Set the id here since the id is not defined in row.node_properties
node.id = row.node_id
RETURN count(*)
Any ideas on how to actually get a query like this to work?
12-22-2022 02:02 PM
You got it, except I made a mistake on the apoc method. The method I gave you converts a json string to a map, when the json string is stored in a node's property. You want to use 'apoc.convert.fromJsonMap' instead .
try this:
// Go over each row in the csv
load csv with headers from "file:///sample.json" AS row
// Initialize the node
CALL apoc.create.node([row.node_label], {})
// Grab a reference to the node we just created (not sure if this is valid)
YIELD node
// Set the node equal to what you mentioned above
set node = apoc.convert.fromJsonMap(row.node_properties)
// Set the id here since the id is not defined in row.node_properties
set node.id = row.node_id
RETURN count(*)
12-22-2022 02:10 PM
Works like a charm, thank you!
All the sessions of the conference are now available online