尝试通过Compass将JSON文件导入MongoDB时出现错误

时间:2020-05-28 23:21:53

标签: json mongodb import mongodb-compass

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

for

以下是我要导入的JSON文件的前几行:

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

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

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

{
    "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",

此错误与其他错误完全相关吗?

1 个答案:

答案 0 :(得分:0)

导入数据按钮需要根据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)