0

I am on Windows 10. I recently obtained a large JSON file (200 MB) via webscraping, and I am now trying to import the file to MongoDB using Compass Community via the import data button. However, whenever I try to import the file, I get the following error:

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

Here are the first few lines of the JSON file I am trying to import:

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

Does anyone have an idea on how I can fix this error?

EDIT: I ran the import again after restarting Compass and got this:

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

Is this error related at all to the other one?

1 Answers1

1

The import data button needs the object to be inlined according to https://docs.mongodb.com/compass/master/import-export/#import-data-into-a-collection.

Apart from that, I had issues with the "Unexpected token : in JSON at position 0", and even tho I could not figure out the cause yet, I tried creating a new .json and copying the content into it, and surprisingly, it worked.

Also, remember to leave a line break at the end of the file.

To convert the json into a 1 line format, you could use the following python script:

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)