cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Auto-create properties from json/REST api?

Dabeeper
Node

Is it possible for neo4j to "auto create" node properties from a JSON/REST endpoint? For example, I can hit a uri and see the resulting JSON. I want to create a node based on one of the key value pairs, which is easy enough. Now there may be 20-30 more key:value pairs at that UNWIND level. Some of which I ultimately want as nodes, some of which I ultimately want as properties. I have gone through and created cypher to do this, but what I am finding is that what I may code for one request, breaks on another request. The value may be null (which can be fixed with a CASE statement) or the key may not exist in another request (which I can kinda fix with do.when). The problem becomes, I don't want to write 30+ CASE or do.when statements as there may be other key/value pairs that I am not aware of, but would still like to capture.

So I am asking if there is a way to create the initial node, then for however many key/value pairs exist for that request, set node properties for each one of those keys (matching the name of that key)
for each value without hard-coding it?
After the API data is gathered, I would then create new nodes/relationships based on the properties using simple MATCH/WHERE/CREATE cypher. I know it probably seems 6 one way/half dozen the other, but I want to make sure that the initial data is captured without intensive troubleshooting on the import.

3 REPLIES 3

Cobra
Ninja
Ninja

Hello @Dabeeper

I am not to sure if I have correctly understood you need but you could use MERGE clause on the id property then use the SET to add other properties:

MERGE (p:Person {id: 0})
SET p = {prop1: "prop1", prop2: "prop2"}

You can have more information here.

Regards,
Cobra

I will try to be more clear. You hit the API endpoint do your various UNWINDs to get a few levels deep to get to the level that has the info. It may look like this for the first hit

"endpoint":
  {"id": "0",
   "prop1":  "Prop1",
   "prop2": "Prop2",
   etc...}

Now if you hit the same endpoint with a different query string, it may return something like this:

"endpoint":
  {"id": "1",
   "prop1":  "Prop1",
   "prop3": "Prop3",
   etc...}

Notice the absence of "prop2" and the addition of "prop3". I could hit it again and find something like this:

"endpoint":
  {"id": "2",
   "prop4":  "Prop4",
   "prop5": "Prop5",
   etc...}

The only for sure common key is endpoint.id, and I have no idea how many endpoint.xxx exist or what they are even named as new ones tend to pop-up, but I want to capture that information when the endpoint is hit and do my clean-up later. I want the cypher to account for the presence of unknown (to me) "props".

I am trying to do this is in straight cypher or apoc to eliminate the need for intermediate file cleaning in a csv, or writing a python script for those who do not have python setup. I am thinking or some sort of FOREACH statement, but I haven't been able to get that to work either.

EDIT: Now that I think about it a bit more, maybe better explained as set the properties on the node using the complete "map". Maybe apoc.map.* is what I am looking for?

Cobra
Ninja
Ninja

Hello @Dabeeper

I think you could use this function from APOC plugin.

Regards,
Cobra