Neo4j How to access node's properties in python

Viewed 4376

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?

4 Answers

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)

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()

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.

Afaik, there is no way to convert Node object of Neo4j driver to a dictionary/json object in python. So you need to do it yourself. Here is a simple way:

def node_to_json(neo4j_node):
    '''
    Convert a neo4j node object into json/dict
    :param neo4j_node:
    :return: node in json/dict format
    '''

    json_version = {}
    for items in neo4j_node.items():
        json_version[items[0]] = items[1]

    return json_version
Related