11

I tried to import a simple json file using mongoimport and i get the following error

PER-MacBook-Pro:/AJ$ mongoimport --db test --collection samplePM --file /users/AJ/Documents/Development/ETLwork/Dummydata/Penguin_Players.json
2015-06-16T09:53:57.291-0400    connected to: localhost
2015-06-16T09:53:57.293-0400    Failed: error processing document #1: invalid character '\\' looking for beginning of object key string
2015-06-16T09:53:57.293-0400    imported 0 documents

Sample json file is as follows:

      {  
         "position":"Right Wing",
         "id":8465166,
         "weight":200,
         "height":"6' 0\"",
         "imageUrl":"http://1.cdn.nhle.com/photos/mugs/8465166.jpg",
         "birthplace":"Seria, BRN",
         "age":37,
         "name":"Craig Adams",
         "birthdate":"April 26, 1977",
         "number":27
      },
      {  
         "position":"Right Wing",
         "id":8475761,
         "weight":195,
         "height":"6' 2\"",
         "imageUrl":"http://1.cdn.nhle.com/photos/mugs/8475761.jpg",
         "birthplace":"Gardena, CA, USA",
         "age":23,
         "name":"Beau Bennett",
         "birthdate":"November 27, 1991",
         "number":19
      }

Am I doing something wrong here?

chridam
  • 100,957
  • 23
  • 236
  • 235
Ak040
  • 171
  • 1
  • 2
  • 7
  • 1
    Seems you have non "UTF-8" characters in there that your sample is not showing –  Jun 16 '15 at 14:15

7 Answers7

7

I was able to get away with using --jsonArray tag, giving the file full path, and modified it adding bracket at the beginning and at the end,

[{"my":"json","file":"imported"},{"my":"cool","file":"succeeded"}]

mongoimport --db myCoolDb --collection myCoolColl --file /path/to/my/imported/file.json --jsonArray
Ardhi
  • 2,855
  • 1
  • 22
  • 31
5

The comment about non "UTF-8" characters was helpful.

It seems like there is a problem with creating json documents using textedit in Mac. I could not find these non UTF-8 characters but i created the same file using vi test.json in mac shell. I pasted the contents, saved the file and used mongoimport. It works now.

Thanks

Ak040
  • 171
  • 1
  • 2
  • 7
  • 1
    Had the same issue - ended up creating the json document in a sublime text - error solved. – zero_cool Jun 24 '15 at 19:19
  • 3
    encoding UTF-8 without BOM works for me http://stackoverflow.com/questions/2223882/whats-different-between-utf-8-and-utf-8-without-bom – Rubén Jan 26 '16 at 11:15
  • Convert your object into an array and then use **--jsonArray** and **--legacy** at the end of your mongoimport url . working fine – Mohd. Shoaib Sep 25 '20 at 04:46
  • In TextEdit, try Format -> Make Plain Text. This goes to plain text from Rich Text, and won't mess up your formatting anymore. – opyate Aug 04 '22 at 10:36
4

I got the same error while importing json data. Instead use the .bson data using mongorestore command.

mongorestore -d <db> -c <collection> <.bson-file>

Use --drop if you want to drop the existing data in the collection.

Winter
  • 341
  • 1
  • 4
  • 8
1

I was getting Failed: error processing document #112783: invalid character ',' looking for beginning of value because one of my objects was formatted improperly. Notice how "psychosurgery" is missing curly braces:

{
  "word": "psychosurgeons",
  "firstLetter": "p"
}
  " psychosurgery",
{
  "word": "psychosurgical",
  "firstLetter": "p"
}

Since there are over 600,000 lines in the file I'm trying to import, this would have been tough to find manually.

So I ran the same mongoimport command with full verbosity (-vvvvv) enabled, and the script stopped right on the problematic object. See mongoimport --help for more info.

Hope this helps someone.

JP Lew
  • 4,121
  • 2
  • 32
  • 45
0

I got the same problem because I used texteditor on the mac. The solution was to convert the file to plain text. Make sure the extension ends in .json because texteditor wants to put .txt at the end.

ThomasDeLange
  • 76
  • 1
  • 6
0

Just open a text file, copy all the data to the newly created text file. While saving the text file select the option 'UTF-8' in the Encoding drop down and later change the text file to JSON or CSV by renaming.

Then import the file as usual as it is.

Ashiq KS
  • 145
  • 2
  • 9
0

if you used mongoexport to download use mongoimport to upload or if you use mongodump to download use mongorestore to upload because i used to download with mongodump and tried to upload with mongoimport i was getting error processing document #1: invalid character '\u008c'error after i tried with mongorestore it was fine

Mike Mulaffer
  • 149
  • 2
  • 6