【问题标题】:I am getting an error when trying to import a JSON file to MongoDB via Compass尝试通过 Compass 将 JSON 文件导入 MongoDB 时出现错误
【发布时间】:2020-05-28 23:21:53
【问题描述】:

我在 Windows 10 上。我最近通过网络抓取获得了一个大型 JSON 文件 (200 MB),我现在正尝试通过导入数据按钮使用 Compass Community 将该文件导入 MongoDB。但是,每当我尝试导入文件时,都会收到以下错误:

Unexpected token l in JSON at position 0 while parsing near 'l

这是我尝试导入的 JSON 文件的前几行:

{
    "bands": [{
            "activity": "Split-up",
            "bandMembers": ["https://www.metal-archives.com/artists/Jon_Powlowski/760544", "https://www.metal-archives.com/artists/Ruben_Martinez/760545", "https://www.metal-archives.com/artists/Greg_Eickmier/416646", "https://www.metal-archives.com/artists/Nedwob/471955"],
            "bandName": "A // Solution",
            "country": "United States",
            "dateAdded": "2018-08-04",
            "genre": "Crust Punk/Thrash Metal",
            "label": {
                "labelName": "Voltic Records",
                "labelUrl": "https://www.metal-archives.com/labels/Voltic_Records/47794"
            },
            "location": "California",
            "lyricalThemes": "N/A",
            "releases": [{
                    "numReviews": 0,
                    "releaseName": "Butterfly",
                    "reviewAverage": null,
                    "type": "EP",
                    "url": "https://www.metal-archives.com/albums/A_--_Solution/Butterfly/723154",
                    "year": "1989"
                }, {
                    "numReviews": 0,
                    "releaseName": "Things to Come",
                    "reviewAverage": null,
                    "type": "EP",
                    "url": "https://www.metal-archives.com/albums/A_--_Solution/Things_to_Come/723155",
                    "year": "1995"
                }
            ],
            "similarArtists": null,
            "url": "https://www.metal-archives.com/bands/A_--_Solution/3540442600",
            "yearFormed": "N/A",
            "yearsActive": "N/A"
        }, {
            "activity": "Active",

有人知道如何解决这个错误吗?

编辑:我在重新启动 Compass 后再次运行导入并得到了这个:

Unexpected token : in JSON at position 0 while parsing near ': null,

这个错误与另一个错误有关系吗?

【问题讨论】:

    标签: json mongodb import mongodb-compass


    【解决方案1】:

    导入数据按钮需要根据https://docs.mongodb.com/compass/master/import-export/#import-data-into-a-collection内联对象。

    除此之外,我还遇到了“意外令牌:在 JSON 中的位置 0”的问题,即使我还无法找出原因,我尝试创建一个新的 .json 并将内容复制到其中,并且令人惊讶的是,它奏效了。

    另外,请记住在文件末尾留一个换行符。

    要将 json 转换为 1 行格式,您可以使用以下 python 脚本:

    import json
    import sys
    import codecs
    import os
    
    
    def read_file(name):
        with open(name, encoding='utf8') as f:
            return f.read()
    
    
    def write_file(name, text):
        os.makedirs(os.path.dirname(name), exist_ok=True)
        with codecs.open(name, "w", "utf-8-sig") as temp:
            temp.writelines(text)
    
    
    text = read_file(sys.argv[1])
    data = json.loads(text)
    
    result = json.dumps(text, ensure_ascii=False) + "\n"
    
    write_file(sys.argv[2], result)
    

    【讨论】:

      猜你喜欢
      • 2022-01-06
      • 1970-01-01
      • 2019-12-28
      • 2017-12-26
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      相关资源
      最近更新 更多