Mongodb - Mongoimport error invalid character

前端 未结 6 2066
时光取名叫无心
时光取名叫无心 2020-12-19 01:35

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         


        
相关标签:
6条回答
  • 2020-12-19 02:03

    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.

    0 讨论(0)
  • 2020-12-19 02:10

    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.

    0 讨论(0)
  • 2020-12-19 02:13

    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
    
    0 讨论(0)
  • 2020-12-19 02:15

    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.

    0 讨论(0)
  • 2020-12-19 02:19

    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

    0 讨论(0)
  • 2020-12-19 02:19

    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.

    0 讨论(0)
提交回复
热议问题