# How to load in nodes from a csv file with a dynamic number of properties

**URL:** https://community.neo4j.com/t/how-to-load-in-nodes-from-a-csv-file-with-a-dynamic-number-of-properties/58799
**Category:** General
**Tags:** migrated
**Created:** [December 22, 2022, 7:04pm UTC](https://community.neo4j.com/t/how-to-load-in-nodes-from-a-csv-file-with-a-dynamic-number-of-properties/58799 "2022-12-22T19:04:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![OmegaTauPhi](https://avatars.discourse-cdn.com/v4/letter/o/8e7dd6/32.png) [@OmegaTauPhi](https://community.neo4j.com/u/OmegaTauPhi)
#### Post date: [December 22, 2022, 7:04pm UTC](https://community.neo4j.com/t/how-to-load-in-nodes-from-a-csv-file-with-a-dynamic-number-of-properties/58799/1 "2022-12-22T19:04:12Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)
#### Post date: [December 22, 2022, 7:33pm UTC](https://community.neo4j.com/t/how-to-load-in-nodes-from-a-csv-file-with-a-dynamic-number-of-properties/58799/2 "2022-12-22T19:33:24Z")

</div>

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/](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.

---

<div class="post-metadata">

### Author: ![OmegaTauPhi](https://avatars.discourse-cdn.com/v4/letter/o/8e7dd6/32.png) [@OmegaTauPhi](https://community.neo4j.com/u/OmegaTauPhi)
#### Post date: [December 22, 2022, 7:10pm UTC](https://community.neo4j.com/t/how-to-load-in-nodes-from-a-csv-file-with-a-dynamic-number-of-properties/58799/3 "2022-12-22T19:10:18Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![OmegaTauPhi](https://avatars.discourse-cdn.com/v4/letter/o/8e7dd6/32.png) [@OmegaTauPhi](https://community.neo4j.com/u/OmegaTauPhi)
#### Post date: [December 22, 2022, 9:36pm UTC](https://community.neo4j.com/t/how-to-load-in-nodes-from-a-csv-file-with-a-dynamic-number-of-properties/58799/4 "2022-12-22T21:36:10Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)
#### Post date: [December 22, 2022, 10:02pm UTC](https://community.neo4j.com/t/how-to-load-in-nodes-from-a-csv-file-with-a-dynamic-number-of-properties/58799/5 "2022-12-22T22:02:48Z")

</div>

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:

```auto
// 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(*)

```

---

<div class="post-metadata">

### Author: ![OmegaTauPhi](https://avatars.discourse-cdn.com/v4/letter/o/8e7dd6/32.png) [@OmegaTauPhi](https://community.neo4j.com/u/OmegaTauPhi)
#### Post date: [December 22, 2022, 10:10pm UTC](https://community.neo4j.com/t/how-to-load-in-nodes-from-a-csv-file-with-a-dynamic-number-of-properties/58799/6 "2022-12-22T22:10:45Z")

</div>

Works like a charm, thank you!
