Skip to content

MongoDB Import JSON Error – Unmarshaling Bytes

You can import data from en external JSON file using the following command. There are multiple ways to import data into MongoDB.

mongoimport --db ordersDB --collection orders --file /home/welcome/Downloads/MOCK_DATA.json

When you are importing data into a MongoDB collection from a JSON file, you would encounter the following error: Failed: error unmarshaling bytes on document #0: JSON decoder out of sync – data changing underfoot?

2019-09-05T10:24:24.401+0530	connected to: localhost
2019-09-05T10:24:24.414+0530	Failed: error unmarshaling bytes on document #0: JSON decoder out of sync - data changing underfoot?
2019-09-05T10:24:24.414+0530	imported 0 documents

How to fix this error?

1. Make sure your JSON is formatted correctly. Use a JSON formatter or a beautifier to validate the JSON.

2. If you date is of type JSON Array, make sure you pass the -jsonArray argument to the mongoimport command.

mongoimport --db ordersDB --collection orders --jsonArray --file /home/welcome/Downloads/MOCK_DATA.json
See also  Convert MongoDB ObjectId to timestamp and vice versa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.