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"
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.

added the --legacy argument and the import proceeded

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.)

To view or add a comment, sign in

More articles by Great Dilla

  • Quality of Service (QoS) for Zoom

    As we go deeper into the pandemic, more and more people start to adapt to the so-called "new normal". Employees work…

    1 Comment
  • Weird SSL Errors with Python PIP

    If you have been working with Python for a while, chances are you are very familiar with its package installer "pip"..

  • Python Dictionaries and the Good Old Square Notation

    Python is the data scientists, data analysts and data engineers language of choice. That statement stirs up a debate as…

  • Compilation of Online Training Courses

    It's been a while since I last wrote an article in LinkedIn. I have been pre-occupied by a few personal pursuits, and…

  • Building Python Skillsets

    Have you ever had the chance to play bowling? If not, try visiting a bowling lane and observe the players and how they…

  • Data Science -- Where to Start?

    I, too, was asking this question, not long ago. I could still remember how the first steps were like.

Others also viewed

Explore content categories