MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Mongoimport error - Failed: cannot decode array into a D

Hi, I’m getting an error using mongoimport (version r4.2.6) saying that it cannot decode array into a D. I’ve already added the --jsonArray option. The json was created by a python API that I’m running from the command line.

here’s the command:
mongoimport --host Bible-shard-0/bible-shard-00-00-pdnuf.mongodb.net:27017,bible-shard-00-01-pdnuf.mongodb.net:27017,bible-shard-00-02-pdnuf.mongodb.net:27017 --ssl --username rsfarrell --password MY_PWD --authenticationDatabase admin --db Sermons --collection Rick_Warren --jsonArray --type json --file rick.json

I’ve attached the error.

Any ideas? I’m happy to send over the json but I’m not able to upload it here. Thanks for any help!

Rich

Hi @Rich_Farrell,

The error related to an array type from the document(s) in the file.
You mentioned that the JSON was created by a Python API. If the documents in the file is consistent in structure, could you provide just one document ?

Regards,
Wan.

Hi Wan, when I drag/drop the file to this JSON validator (https://jsonlint.com/), it says that it is a valid JSON.

Hi Wan, I’ve attached the image of the json file. Is there a way for me to send you the actual json? If I edit the file down myself then mongo imports it properly (I’m using Compass / Add Data / Import File from within the Collection). The file size is 21k.

Hi Wan, 2nd part of json (I’m only able to upload 1 image at a time).

Hi @Rich_Farrell it would probably help if you put the file in a pastebin, gist or something similar (if possible) so @wan can download and work with the actual data.

1 Like

Brilliant - thank you!

@wan - here is the pastebin - https://pastebin.com/hUpjsnDY

Hi @Rich_Farrell,

Thanks for providing a sample file. Looking at the file, the problem with the json file is that it contains an array of array documents. Currently this is the structure of your file:

[ [ {"text":"one"}, {"text":"two"} ] ]

The format should be as below:

[ {"text":"one"}, {"text":"two"} ]

Try updating your Python script to output an array of dictionaries instead of an array of array of dictionaries, and try running mongoimport again.

Regards,
Wan.

2 Likes

Personally, I would modify the Python code to output one document per line as it is done by default by mongoexport. Benefits

  1. No need to use --jsonArray option
  2. wc -l gives the number of documents.
  3. You can also use grep or other line oriented tools.
1 Like

Thanks Wan and @steevej …I’ve emailed the programmer to ask if he can make your recommended changes.

1 Like