Using py2neo to connect to neo4j DB: How can I convert "class 'py2neo.database.Record'" to a dictionary or list in python?
Asked
Active
Viewed 2,330 times
2 Answers
3
You can directly convert a Record
to a list:
result = graph.cypher.execute('MATCH (n) RETURN n')
a_record = result[0] # -> this is a Record object
list_of_things_in_record = list(a_record)
print(list_of_things_in_record)

Martin Preusse
- 9,151
- 12
- 48
- 80
-
Same applies for `dict` - you can wrap a `Record` in the function. – Nigel Small May 19 '16 at 08:11
-
@NigelSmall If you were using py2neo3 with the cursor, how would you do it? – steven.levey Jun 08 '16 at 06:48
-
1
Seems the data()
-Method in any neo4j.data.Record
can convert it into a dict
.
with self.driver.session() as session:
records = session.write_transaction(self._return_nodes, cypher)
rdict = [rec.data() for rec in records]
return rdict
@staticmethod
def _return_nodes(tx, cypher):
return [rec for rec in tx.run(cypher)]

Qohelet
- 1,459
- 4
- 24
- 41