MongoDB Import Error "Failed: invalid JSON input"
While working on a MongoDB database migration recently, I was expecting the lift-and-shift activity to be smooth sailing. It is not exactly "lift-and-shift" per-se as the activity was not on-premise to cloud, but rather cloud-to-cloud. However, the steps performed are similar to that of a lift-and-shift.
After careful planning and procedure verification, the confidence to that effect was certain -- this is not being done for the first time, and the procedure has been implemented several times in the past. Executed successfully, if I may add.
The database export was uneventful as expected. I couldn't say the same for the import. The database I was working on is the popular NoSQL database MongoDB.. and the unforeseen error "Failed: invalid JSON input". Screenshot is provided below.
mongoimport error: "Failed: invalid JSON input. Position: 230. Character: N"
The error itself doesn't make sense. I checked the exported file and at character position 230, the file contains a different character than "N".
This error is actually encountered often on several forums. At first, I tried to debug with verbosity flags to gather clues as to what might be happening with the import that is causing the error.
As seen on the screenshot, increasing the debug verbosity doesn't indicate the error, even at the highest debug levels.
I used one of my lifelines (phone-a-friend). And with a simple question the answer was found. My lifeline asked if the versions of MongoDB source and target are the same.
When different versions are used there might be a need for the legacy argument to properly import the database export to the target database.
The --legacy argument is highlighted in the screenshot.
If you get this error or something similar, database versions might be a good angle to look at. In my experience this was indeed the cause.
Supplementary lesson learned in this experience: ask the right questions (or learn to ask the right questions)
(I wish to give credit to my lifeline, but he wishes to remain anonymous.)