jsonmongodbmongoimport

mongoimport failed to import JSON data


I'm trying to import a file called breedData.json into mongodb following the steps in this article http://zaiste.net/2012/08/importing_json_into_mongodb/.

So I enter the command below in terminal from my app's root folder. The breedData.json file is also in the app root folder. Mongod and nodemon are running.

mongoimport --db shelterdoggie --collection breeds --type json --file breedData.json --jsonArray

and I get this:

2015-09-07T00:58:18.646-0700    connected to: localhost
2015-09-07T00:58:18.647-0700    Failed: error reading separator after document #2: bad JSON array format - found '{' outside JSON object/array in input source
2015-09-07T00:58:18.647-0700    imported 0 documents

I've checked my file with jsonlint.com and it is valid json.

I've tried using this json format with the above Terminal command:

[
{"malehw":"Ht: 9-11.5, Wt: 7-9","femalehw":"Ht: 9-11.5, Wt: 7-9"},
{"malehw":"Ht: 27, Wt: 60","femalehw":"Ht: 25, Wt: 50"},
{"malehw":"Ht: 23, Wt: 45","femalehw":"Ht: <23, Wt: 45"}
]

and I've also tried this format, while leaving off the --jsonArray flag in the command above:

{"malehw":"Ht: 9-11.5, Wt: 7-9","femalehw":"Ht: 9-11.5, Wt: 7-9"}
{"malehw":"Ht: 27, Wt: 60","femalehw":"Ht: 25, Wt: 50"}
{"malehw":"Ht: 23, Wt: 45","femalehw":"Ht: <23, Wt: 45"}

but then I get this error:

2015-09-07T01:11:00.034-0700    connected to: localhost
2015-09-07T01:11:00.035-0700    Failed: error processing document #1: invalid character '{' after array element
2015-09-07T01:11:00.035-0700    imported 0 documents

I've checked the file and there are 152 {'s and 152 }'s and I have 152 rows/documents in my file, so it shouldn't be due to an unclosed curly brace somewhere. I don't understand why this isn't working. If you have any suggestions, I'd appreciate it.

Here's one full line from my .json file in case it helps:

{"malehw":"Ht: 24-26, Wt: 75-95","femalehw":"Ht: 22-24, Wt: 75-95","catfriendly":"••••","easytraining":"••••••","watchdog":"••••••","grooming":"•••","coldtolerant":"••••","care":"Among the most intelligent of breeds, the German Shepherd Dog is so intent on his mission whatever that may be and he is virtually unsurpassed in working versatility. He is utterly devoted and faithful. He is usually good with other pets.","breed":"German Shepherd Dog","health":"This breed needs daily mental and physical challenges. He enjoys a good exercise session as well as learning session. He is family-oriented and does well as a house dog. His coat needs brushing one or two times weekly.","energy":"••••","playfulness":"•••","dogfriendly":"••","strangerfriendly":"•••","protection":"••••••","heattolerant":"••••","exercise":"•••••","affection":"••••","index":67,"url":"https://www.petfinder.com/dog-breeds/German-Shepherd-Dog"}

Solution

  • Clement's suggestion put me on the right track. Putting the full file path for the file worked. The import command in Terminal looked like this:

    mongoimport --db shelterdoggie --collection breeds --type json --file ~/dev/shelter_doggie/breedData.json --jsonArray
    

    As for my csv import attempt, I only had one dash before db instead of two dashes. I fixed that and added the full file path for the csv file. So it works when I run the command like this:

    mongoimport --db shelterdoggie --collection breeds --type csv --file ~/dev/shelter_doggie/kimonoData.csv --headerline
    

    Not sure if there's another way to do this without the full file path (as the article in my initial question indicates), but at least I can import my data.