22

I want to import data of type JSON in MongoDB compass, the import function gives this error " unexpected end of JSON input "

enter image description here

there is a some of my JSON file

[
   {
      "id":4,
      "user":"test@example.com",
      "date1":"2019-03-01",
      "date2":"2019-04-01",
      "statut":"Good",
      "guest_number":4
   }
]
OAH
  • 1,160
  • 2
  • 11
  • 24

12 Answers12

25

the solution is to write all JSON in one line, but if we have a big doc !! I just found a solution that I can import data with this command in terminal :

mongoimport --jsonArray --db YourDatabase --collection YourCollection --file Yourfile.json
OAH
  • 1,160
  • 2
  • 11
  • 24
  • How i can connect to remote db – Rahul Mahadik Jun 11 '19 at 11:43
  • 1
    @RahulMahadik you can provide --uri with connection string parameter for remote db. https://docs.mongodb.com/manual/reference/program/mongoimport/#cmdoption-mongoimport-uri – Mathias Jan 30 '20 at 14:04
  • For me json wasn't in an array. So I had to remove `--jsonArray`. – Mathias Jan 30 '20 at 14:06
  • 1
    I got a 'Missin semicolon' error upon executing this. – Eli Zatlawy Mar 06 '22 at 19:16
  • If you get a 'Missing semicolon' error, then you probably are using mongosh. But you need to run the command directly in the terminal, and also make sure that you've installed the MongoDB Database Tools, cause mongoimport is one of these tools. – Adel Abdellatif Mar 04 '23 at 18:15
13

I had this issue 6 month ago, the solution is write all JSON in one line. [{"id":4,"user":"test@example.com","date1":"2019-03-01","date2":"2019-04-01","statut":"Good","guest_number":4}]

MongoDB Compass will told you:

Import success!

But definitely the document will not appear in your collection, so better use Robo3T if you gonna insert json. Then you can use again Compass like I do. It is weird, yes, but I didnt found other solution yet.

[UPDATE]

I achieve import data with Compass, but I achieve exporting first a document from Compass to see how it write the json.

{"_id":{"$oid":"5e4cf105c9ba1a21143d04a2"},"tPreguntas":["Pregunta 1","Pregunta 2","Pregunta 3","Pregunta 4","Pregunta 5"],"tCategorias":[],"tPublico":true,"tFechaCreacion":{"$date":{"$numberLong":"1582100741716"}},"tCodigo":"test1","tTitulo":"Test 1","tDescripcion":"Test de muestreo número uno para comprobar.","tCreadoPor":"eoeo@eoeo.com"}

It look to different to the json online I have post in my first post. (look that objectId "$oid" for example). So if you follow that pattern Compass will import you fine.

Schwarz54
  • 964
  • 1
  • 9
  • 18
  • @KristoffDT Did you tried before vote me down? Try it before you downvoting someone. Your solution dont work. I correct his document putting the double quotes and I explain the problem and test it. My solution works. – Schwarz54 May 15 '19 at 14:18
  • Correct, I did not I was too hasty, yet single quotes will never work with json. I did remove my down-vote, sorry for that – KristoffDT May 15 '19 at 14:19
5

This parsing error can be solved using minification. So, minify json like this. Although, it is quite a hectic process to do this for each object.

And this kind of minification like this worked for me.

{ 
    "_id" : ObjectId("5b9ecf9a64f634289ca895bb"), 
    "name" : "Mark"
}
{ 
    "_id" : ObjectId("5b9edd9064f634289ca895e4"), 
    "name" : "David"
}

To :

{"_id":"ObjectId(\"5b9ecf9a64f634289ca895bb\")","name":"Mark"}
{"_id":"ObjectId(\"5b9edd9064f634289ca895e4\")","name":"David"}
prisar
  • 3,041
  • 2
  • 26
  • 27
3

This parsing error can be solved using minification. So, minify json like this. Although, it is quite a hectic process to do this for each object.

{
"_id" : "123456",
"name" :  "stackoverflow"
}

change to :

{"_id":"123456","name":"stackoverflow"}
prisar
  • 3,041
  • 2
  • 26
  • 27
3

Just copy the contents of your json file then in Mongodb Compass select your database then click on Add Data which will drop down then click on insert document a dialog pops up then paste it in there and click insert.

nivla360
  • 973
  • 14
  • 21
1

This answer here Solution solved the issue for me. It seems to be a formatting issue.

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – DjSh Nov 19 '19 at 20:29
1

It's an issue with the end-of-line characters (EOL).

In a Windows environment line terminations are normally CR NL (\r\n), while MongoDB Compass seems to only support CR (\r).

You can open the file in Notepad++, enable the "Show all characters" toggle in the toolbar and inspect your current end-of-line character.

To fix the issue, select Edit > EOL Conversion > Macintosh (CR).

vi3x
  • 270
  • 2
  • 13
0

The structure of your JSON is incorrect, you might want to read info regarding JSON standards

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

try using double quotes instead of single ones:

JSON validators could help you aswell

[
    {
     "id" : 4,
     "user" : "test@example.com",
     "date1" : "2019-03-01",
     "date2" : "2019-04-01",
     "statut" : "Good",
     "guest_number" : 4
    }
]
KristoffDT
  • 471
  • 3
  • 14
0

I had a similar issue but it turned out to be additional line feeds at the end of the file. Removing these fixed the issue. I suggest opening your file in an editor that shows line feeds e.g. Notepad++

NickC
  • 3
  • 1
0

Add --jsonFormat=canonical to your mongoexport script:

mongoexport --db=quotes --collection=quotes  --jsonFormat=canonical --out=data/quotes.json

JSON can only directly represent a subset of the types supported by BSON. To preserve type information, MongoDB adds the following extensions to the JSON format.

Source

Community
  • 1
  • 1
-1

You can also use the command line of mongodb like this :

db.user.insert(
[
    {
     "id" : 4,
     "user" : "test@example.com",
     "date1" : "2019-03-01",
     "date2" : "2019-04-01",
     "statut" : "Good",
     "guest_number" : 4
    },
    {
     "id" : 5,
     "user" : "test2@example.com",
     "date1" : "2019-03-01",
     "date2" : "2019-04-01",
     "statut" : "Good",
     "guest_number" : 4
    }
]
-1

Run this command in cmd and the cmd path should be in the same folder where the JSON file occurs.

mongoimport --jsonArray --db YourDatabase --collection YourCollection --file Yourfile.json
Ahmer Shahid
  • 107
  • 1
  • 4