首页 > 解决方案 > 如何使用密码查询语言将未嵌套的 json 数据解析到 Neo4j 数据库?

问题描述

在下面的 json 文件中,我想访问“934934507945312256”、“934934503604174848”、....然后是其中的键。但是在使用UNWIND 子句后,我无法访问这些键的数据(quote_count,reply_count 等),因为这些键(“934934507945312256”,“934934503604174848”,...)是随机生成的。

    {
    "934934507945312256": {
    "quote_count": 0,
    "reply_count": 0,
    "hashtags": null,
    "datetime": "2017-11-26 23:58:51",
    "date": "2017-11-26",
    "like_count": 0,
    "verified": "False",
    "sentiment": 0,
    "author": "JudyThe Resistance",
    "location": "Hollywood, California USA",
    "tid": "934934507945312256",
    "retweet_count": 0,
    "type": "retweet",
    "media_list": null,
    "quoted_source_id": null,
    "url_list": null,
    "tweet_text": "RT @kylegriffin1: Reminder: The Senate Judiciary Committee gave Jared Kushner a November 27 deadline to turn over the missing records… ",
    "author_profile_image": "https://pbs.twimg.com/profi...",
    "author_screen_name": "jgirl66",
    "author_id": "23737528",
    "lang": "en",
    "keywords_processed_list": [
    "reminder",
    "senate judiciary committee",
    "kushner november",
    "deadline"
    ],
    "retweet_source_id": "934872065471115264",
    "mentions": [
    "kylegriffin1"
    ],
    "replyto_source_id": null
    },
    "934934503604174848": {
    "quote_count": 0,
    "reply_count": 2,
    "hashtags": [
    "MissUniverse",
    "Thailand"
    ],
    "datetime": "2017-11-26 23:58:50",
    "date": "2017-11-26",
    "like_count": 38,
    "verified": "False",
    "sentiment": 0,
    "author": "P'Hmee7.5",
    "location": "Bangkok, Thailand",
    "tid": "934934503604174848",
    "retweet_count": 105,
    "type": "Tweet",
    "media_list": null,
    "quoted_source_id": null,
    "url_list": null,
    "tweet_text": "รอโหวต มรญ #MissUniverse #Thailand",
    "author_profile_image": "
    Thumbnail
    ",
    "author_screen_name": "Peehmee75",
    "author_id": "700720806972624897",
    "lang": "th",
    "keywords_processed_list": null,
    "retweet_source_id": null,
    "mentions": null,
    "replyto_source_id": null
    },
    "934934336381636608": {
    "quote_count": 0,
    "reply_count": 0,
    "hashtags": null,
    "datetime": "2017-11-26 23:58:10",
    "date": "2017-11-26",
    "like_count": 0,
    "verified": "False",
    "sentiment": 0,
    "author": "selfresqingprncess",
    "location": "Maine, USA",
    "tid": "934934336381636608",
    "retweet_count": 0,
    "type": "retweet",
    "media_list": null,
    "quoted_source_id": null,
    "url_list": null,
    "tweet_text": "RT @kylegriffin1: Reminder: The Senate Judiciary Committee gave Jared Kushner a November 27 deadline to turn over the missing records… ",
    "author_profile_image": "https://pbs.twimg.com/profi...",
    "author_screen_name": "slfresqngprncss",
    "author_id": "100536014",
    "lang": "en",
    "keywords_processed_list": [
    "reminder",
"keywords_processed_list": [
            "reminder",
            "senate judiciary committee",
            "kushner november",
            "deadline"
        ],
        "retweet_source_id": "934872065471115264",
        "mentions": [
            "kylegriffin1"
        ],
        "replyto_source_id": null
    }
  }

我已经尝试过:-
query =
"""
with {json} as data
UNWIND data as doc
FOREACH( l in doc|
MERGE (label1:Label1 {author:l.author}))
"""

但我收到错误:-
无法使用作者的空属性值合并节点。

标签: jsonpython-2.7neo4jcypher

解决方案


  1. 您的 JSON 文件格式不正确。

    JSON 字符串不能包含控制字符(如回车符或换行符)。并且您的 JSON 包含一个无关部分(可能是复制/粘贴错误):

    "keywords_processed_list": [
    "reminder",
    
  2. 此外,UNWIND只能与列表一起使用。您不能使用它从地图中获取属性。修复 JSON 错误后,请尝试以下查询(我假设这json是一个参数,其值为您的 JSON 数据):

    UNWIND KEYS($json) AS k
    MERGE(label1:Label1 {author: $json[k].author})
    

推荐阅读