使用Py2Neo将JSON导入Neo4J时类型不匹配

时间:2018-08-02 14:35:36

标签: python-3.x neo4j cypher py2neo

我有一个JSON文件,其头看起来像这样:

"exports": {
        "type": "WordsAndPhrases",
        "date": "2018-08-02T10:07:58.047669Z",
        "relevantYears": "2012,2013,2014,2015,2016,2017",
        "Words": {
            "H1": "WORDS AND PHRASES:",
            "Word": [
                {
                    "Phrase": {
                        "id": "phrase_2011001932",
                        "title": "A common"
                    },
                    "Document": "Law of Property Act 1925, s 193(1) (as amended)",
                    "Refs": {
                        "CaseTitle": {
                            "id": "2011201246",
                            "title": "ADM Milling Ltd v Tewkesbury Town Council"
                        },
                        "title": "None",
                        "citations": "Lewison J [2011] EWHC 595 (Ch); [2012] Ch 99; [2011] 3 WLR 674, Ch D"
                    }
                },

我正在使用以下脚本将JSON数据导入Neo4J:

import json
from py2neo import Graph, authenticate

authenticate("localhost:7474", "neo4j", "foobar")
graph = Graph()

with open('wp.json') as data_file:
    json = json.load(data_file)

query = """
WITH {json} AS document

UNWIND document.exports.Words.Word AS Word

MERGE (Phrase:a {phrase: Word.Phrase.title})
MERGE (Document:b {document: Word.Document})
FOREACH (case in Word.Refs.CaseTitle.title | MERGE (Report:z {report: case}))
"""

# Send Cypher query.
print (graph.run(query, json = json).dump())

前两个MERGE查询工作正常。但是,FOREACH查询被证明是有问题的。我正在使用FOREACH查询来处理单个块中具有多个CaseTitle属性的实例,例如:

{
                    "Phrase": {
                        "id": "phrase_2011002042",
                        "title": "Acts contrary to purposes and principles of United Nations"
                    },
                    "Document": "Council Directive 2004/83/EC, art 12(2)(c)",
                    "Refs": [
                        {
                            "CaseTitle": {
                                "id": "2011201814",
                                "title": "Federal Republic of Germany v B"
                            },
                            "title": "None",
                            "citations": "(Joined Cases C-57/09 and C-101/09); [2012] 1 WLR 1076, ECJ"
                        },
                        {
                            "CaseTitle": {
                                "id": "2016008987",
                                "title": "Commissaire général aux réfugiés et aux apatrides v Lounani"
                            },
                            "title": "None",
                            "citations": "EU:C:2017:71; [2017] 4 WLR 52, ECJ"
                        }
                    ]
                },

运行脚本时,发生以下错误:

py2neo.database.status.CypherTypeError: Type mismatch: expected a map but was List{Map{title -> String("None"), CaseTitle -> Map{title -> String("Federal Republic of Germany v B"), id -> String("2011201814")}, citations -> String("(Joined Cases C-57/09 and C-101/09); [2012] 1 WLR 1076, ECJ")}, Map{title -> String("None"), CaseTitle -> Map{title -> String("Commissaire général aux réfugiés et aux apatrides v Lounani"), id -> String("2016008987")}, citations -> String("EU:C:2017:71; [2017] 4 WLR 52, ECJ")}}

JSON似乎有效。谁能推荐解决此错误的方法?

1 个答案:

答案 0 :(得分:0)

您应该使用Word.Refs.CaseTitle.title而不是Word.Refs[0].CaseTitle.title。在您的JSON中,很明显refs是一个数组,您将其视为对象。错误消息是这样说的-当它尝试取消引用“引用”下的“ CaseTitle”时,它在等待一张地图,但是您给它的是一张地图列表。