I have many mongodb instance files like below;
[
{
"_id" : ObjectId("35d455de983c0e6a53ea0848"),
"createdAt" : ISODate("2019-12-05T23:25:04.347+0000"),
"__v" : NumberInt(0)
},
{
"_id" : ObjectId("1ecbe0f75df8ccd52a7b1662"),
"createdAt" : ISODate("2019-12-17T12:40:53.521+0000"),
"__v" : NumberInt(0)
}
]
I couldn't import these files because of invalid format.
mongoimport --db DATABASENAME --collection COLLECTIONNAME --file filename.json --jsonArray
And it says;
Failed: invalid JSON input. Position: 16. Character: O
Is there any other way to import those files?
If not, how can I convert them to be imported?
Third, use the following command to import the books.json file into the MongoDB database server: First, start with the mongoimport program name. Next, specify the path to the books.json data file. In this example, it is c:\data\books.json.
The mongoimport shell is unable to find the location of (.json) file. And also there is mistake like --jsonArray. As per MongoDB BOL--jsonArrayModifies the output of mongoexportto write the entire contents of the export as a single JSONarray. By default mongoexportwrites data using one JSONdocument for every MongoDBdocument.
We can't import such data even in Mongo Compass. But fortunately, we can import/export database in various formats in Studio 3T for MongoDB. I think Studio 3T is better than Mongo Compass, but it is not free. After 30 days, we can't use Studio 3T trial version.
As per MongoDB BOL--jsonArrayModifies the output of mongoexportto write the entire contents of the export as a single JSONarray. By default mongoexportwrites data using one JSONdocument for every MongoDBdocument. Note:-Accepts the importof data expressed with multiple MongoDBdocuments within a single JSONarray.
you can use --legacy
option of mongoimport
in order to import the file with the format you want with ObjectId("an oid"), NumberInt(an int) or new Date("an iso date" or any mongo extension available in extended json v1 : https://docs.mongodb.com/manual/reference/mongodb-extended-json-v1/)
invalid JSON input. Position: 96. Character: N
For me it was "N" for NaN that I replaced with 0 and it wroked.
It also worked with adding the --legacy option, but I preferred to fix the file as the legacy option did not work within my docker container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With