Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoimport Error, Failed: invalid JSON input

Tags:

mongodb

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?

like image 732
Diamond Avatar asked Dec 17 '19 19:12

Diamond


People also ask

How do I import a JSON file into MongoDB?

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.

What is wrong with mongoimport - --jsonarray?

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.

Is it possible to import data from MongoDB to Studio 3T?

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.

What is jsonarray in MongoDB Bol?

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.


2 Answers

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

like image 164
Vincent Couturier Avatar answered Sep 20 '22 17:09

Vincent Couturier


invalid JSON input. Position: 96. Character: N

For me it was "N" for NaN that I replaced with 0 and it wroked.

enter image description here

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.

like image 20
Felix Furtmayr Avatar answered Sep 18 '22 17:09

Felix Furtmayr