How to get the response of a cypher request in neo4j using python Rest api

I am using Python to access neo4j and create nodes. Before I create a node, I want to check if it exists. I ran this query:

          "query" : "match (PPnode:Node) return PPnode"

      

And using the request library method:

           r.text

      

I am getting a string with the response of my POST request. My question is if there is a more "elegant" way to check if existing nodes with a specific name exist using python and rest api.

This is my code:

   import requests
   import json
   import csv

  headers = {'content-type': 'application/json'}
  url = "http://localhost:7474/db/data/cypher"


  fparts = open('FOC.csv')
  csv_pseudo = csv.reader(fparts)


  for row in csv_pseudo:

   # query to check if node exists
   checkNode = {"query" : "match (PPnode:Node) return PPnode"}
   mkr =requests.post(url, data=json.dumps(checkNode), headers=headers)

      

Thanks Dimitris

+3


source to share


1 answer


I think you could work harder here than you need to. There's a library out there called py2neo that will do what you want to do much easier. If you used it, you could return actual objects instead of raw JSON, which would be easier to deal with:

From the documentation on how to run Cypher queries :



from py2neo import Graph
graph = Graph("http://nifty-site:1138/db/data/")
results = graph.cypher.execute("match (PPnode:Node) return PPnode")

for r in results:
    # get the node you return in your query
    ppNode = r[0]
    # get the properties of your node
    props = ppNode.get_properties()
    # Do nifty stuff with properties, not JSON.

      

+5


source







All Articles