Neo4j How to access node's properties in python
Asked Answered
F

5

9

I'm able to make a query to a graph database like this

from neo4j import GraphDatabase

#establish connection
graphdp = GraphDatabase.driver(uri="bolt://localhost:7687", auth=("neo4j","Python"))

session = graphdp.session()

q1="MATCH (n {id:0}) return n"
nodes = session.run(q1)

for node in nodes:
    print(node)

The result is:

<Record n=<Node id=5 labels={'Ubuntu1604'} properties={'host_image': 'qsrf-56fh-3db5-xd4t', 'id': 0}>>
<Record n=<Node id=6 labels={'Ubuntu1804'} properties={'host_image': 'qsrf-56fh-3dd4-44ty', 'id': 0}>>
<Record n=<Node id=7 labels={'Network'} properties={'start': '', 'capability': 'connection', 'cidr': '', 'end': '', 'nameservers': '[10.0.71.254, 8.8.4.4, 8.8.8.8]', 'id': 0}>>
<Record n=<Node id=8 labels={'Port'} properties={'port_ip': '', 'requirement': '["container","connection"]', 'id': 0}>>
<Record n=<Node id=13 labels={'GuestLinuxUser'} properties={'id': 0, 'playbook': 'createLinuxUser'}>>
<Record n=<Node id=16 labels={'GuestWindowsUser'} properties={'id': 0, 'playbook': 'createWindowsUser'}>>

Process finished with exit code 0

How can I access each node property?

Fiddler answered 4/3, 2020 at 10:27 Comment(0)
S
11

You can save BoltStatmentResult object data out and then access the node properties via the Node.get() method:

q1="MATCH (n {id:0}) return n"
nodes = session.run(q1)
results = [record for record in nodes.data()]

# Now you can access the Node using the key 'n' (defined in the return statement):
res[0]['n'].get('host_image')

I named the element 'record' in nodes.data() iteration, because if your RETURN had more than one item returned, then record != node. It's a dictionary of items in the RETURN.

You can then access any of the methods of the Node Data Type, here's the docs reference

E.g:

node = res[0]['n']
labels = list(node.labels)
Surratt answered 4/3, 2020 at 10:45 Comment(6)
thanks for reply but I still have the same problem. If I print (results[0]) I get <Record n=<Node id=5 labels={'Ubuntu1604'} properties={'host_image': 'qsrf-56fh-3db5-xd4t', 'id': 0}>>. How do I access for example "host_image" value?Fiddler
@cristina, sorry I've updated my answer to show how to return a Node and access a propertySurratt
do you know how to retrieve properties like labels or node id? .get() method retrieves from properties={ } but how can i access labels={} or Node id?Fiddler
Updated. Also, it is going back a while, but using the internal ID's was not recommended as they can be reused. I would recommend assigning your own GUUID property as an ID to use.Surratt
Labels can be accessed via node.labels neo4j.com/docs/api/python-driver/1.7/types/…Uniformity
@cristina - has that answered your question?Surratt
B
2

As I can see in the neo4j package, the class Node inherit from the class Entity, which possess a _properties attribute. The problem is that this attribute is not accessible from outside the class Node scope.

To solve this problem, you can define yourself the getter :

def get_properties(self):
    return self._properties

and bind this method to your Node instance :

node.get_properties = types.MethodType(get_properties, node)
node.get_properties()

This way, you don't have to change anything in the lib.

Boaster answered 3/1, 2022 at 19:22 Comment(1)
See the comment below from @jollyroger that uses neo4j_node.items() to create a dict. that approach is more mainstream than using MethodType. Although the MethodType approach is clever.Centroid
A
0

As far as I know, there is no direct way to convert Node object into a dictionary object. So you need to do it yourself. Here is a simple way:

def node_to_dict(neo4j_node):
    '''
    Convert a neo4j node object into dict
    :param neo4j_node:
    :return: node properties in a dict
    '''

    props = {}
    for key, value in neo4j_node.items():
        props[key] = value

    return props
Arsenate answered 10/4, 2022 at 10:46 Comment(0)
V
0

To access the properties of a node, we need to access the "neo4j._data.Record" object that we get after running the query.

qry = "MATCH (n) RETURN n LIMIT 2" 
    result = session.run(qry) 
    for record in result: 
        data = record['n']['node_name']

In the above code snippet, "record" is the "neo4j._data.Record" object, and to access a property, you just need to pass the property name in place of 'node_name.

Vallation answered 1/4, 2023 at 7:53 Comment(0)
C
-1

The attribute "properties" is private in the class "Node" (The class is inside "neo4j/graph/init.py") I have added the following method in the class "Node":

def get_properties(self):
    return self._properties

Then you can access the properties by instance_of_your_node.get_properties()

Centerpiece answered 14/8, 2021 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.