【问题标题】:TypeError: <neo4j.work.result.Result object at 0x7f3f4bd01470> is not JSON serializableTypeError: <neo4j.work.result.Result object at 0x7f3f4bd01470> is not JSON serializable
【发布时间】:2021-03-02 20:52:06
【问题描述】:

对于下面的代码,我收到一个错误,请告诉我如何解决这个问题

class GenerateQuery:

    @staticmethod
    def get_nlg(graph_query):
#        graph = Graph("http://localhost:7474",auth=("neo4j", "pass"))
#        graph_response = graph.evaluate(graph_query)
#        return graph_response
        driver = GraphDatabase.driver("neo4j://localhost:7687", auth=("neo4j","pass"))
        with driver.session() as session:
            graph_response = session.run(graph_query)
            return graph_response

    @staticmethod
    def product_review(summary_comp,prod_comp):
        """
        :param summary_comp: product summary
        :param prod_comp: product node name
        :return: Summary/Review of the corresponding product
        """
        query = u'MATCH(s:Store)<-[r:REVIEWED]-(c:Customer) RETURN s.name as ProductName, r.summary as ProductReview'
        graph_response = GenerateQuery.get_nlg(query)
        return graph_response

当上面的结果传递给下面的代码时,会报错:

class ProductReview(Action):
    def name(self):
        return "action_review"

    def run(self, dispatcher, tracker, domain):
        intent = tracker.latest_message['intent']
        summary_comp = tracker.get_slot('summary')
        prod_comp = tracker.get_slot('node')
        graph_response = GenerateQuery.product_review(summary_comp,prod_comp)
        dispatcher.utter_message(json.dumps(graph_response))

错误是:

Traceback (most recent call last):
  File "/home/sangeetha/Desktop/RiQue/venv/lib/python3.6/site-packages/sanic/app.py", line 939, in handle_request
    response = await response
  File "/home/sangeetha/Desktop/RiQue/venv/lib/python3.6/site-packages/rasa_sdk/endpoint.py", line 112, in webhook
    return response.json(result, status=200)
  File "/home/sangeetha/Desktop/RiQue/venv/lib/python3.6/site-packages/sanic/response.py", line 210, in json
    dumps(body, **kwargs),
TypeError: <neo4j.work.result.Result object at 0x7f3f4bd01470> is not JSON serializable

【问题讨论】:

  • 我假设您使用的是官方的 Python Bolt 驱动程序 (github.com/neo4j/neo4j-python-driver),对吗?您使用的 Python 驱动程序是什么版本?在任何情况下,Result 都保存着事务绑定的数据并且不能被序列化。您需要在事务关闭之前提取数据,然后必须对提取的数据进行序列化。

标签: python json neo4j rasa neo4j-driver


【解决方案1】:

Result 并不是要被序列化的,它保存着在事务终止时释放的事务绑定数据。

您必须先提取数据,然后再对其进行序列化。 您可以使用以下内容更改get_nlg

return [record.data() for record in graph_response]

附带说明,session.run 最好替换为 session.read_transaction(又名a transaction function)。

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 2019-05-06
    • 2019-10-09
    • 2014-10-14
    • 1970-01-01
    • 2020-06-29
    • 2019-02-13
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多