Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Python bolt driver: how to convert the result to Json?

I am using the bolt driver(1.0.1) with python. How can I convert the result into a Json so that I can return it to through a flask app? That is I need to convert datatype, "neo4j.v1.types.Record" to "json".

I tried this

from flask import Flask
from neo4j.v1 import GraphDatabase, basic_auth
import json

driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j","neo4j"))
session = driver.session()
app = Flask(__name__)


@app.route('/hello/<prop>')
def hello_name(prop):
  result = session.run("MATCH ...") #this works perfectly fine and the data is captured in result
  session.close()
  for record in result:
    return json.loads(record)

This throws an error :- TypeError: the JSON object must be str, not 'Record'

like image 996
pHM Avatar asked Oct 17 '22 13:10

pHM


1 Answers

The neo4j driver gives the result in a generator.(Record Type) You cannot do json.load as it is not a json serialized string.

What you can do :

for record in result:
    print result[0]

See the format of the result and take the elements you like.

For further understanding on how to parse neo4j records: http://neo4j.com/docs/api/python-driver/current/session.html

like image 163
Satyadev Avatar answered Oct 21 '22 04:10

Satyadev