Jack Daniel
Jack Daniel

Reputation: 2611

Neo4j - SET list of labels to a node using CYPHER, reading the list from json file

I have a basic operation to do with neo4j Cypher from py2neo. I have to read objects from json, where json object structure looks like

{ "ip":"0.0.2.2",
  "location":"uk",
  "uptime":"30",
  "services:["irqbalance","IPsec","nfsd","iscsi","rdisc","iscsi","irqbalance","sssd"]}

Observe that the property services have list of values, where I need to use them as labels.

Here is my approach, where I am able to load json without labels, but unable to set the labels.

My query:

With {data} As machines
UNWIND machines.Servers as server
MERGE (a{ip:server.ip,uptime:server.uptime,location:server.location})

with this query I populated the nodes, but how to set the labels in the same query.

Upvotes: 0

Views: 643

Answers (1)

antikantian
antikantian

Reputation: 620

You cannot set a label from a parameter. Instead, you should format the string with the labels.

labels = ['Some', 'List', 'With', 'Your', 'Labels']
labels = ':'.join(labels)

query = (
    "With {data} As machines "
    "UNWIND machines.Servers as server "
    "MERGE (a:" + labels + ")"
)

Output:

In [22]: query
Out[22]: 'With {data} As machines UNWIND machines.Servers as server MERGE (a:Some:List:With:Your:Labels)'

Upvotes: 1

Related Questions