Unexpected end of JSON input with MongoDB Compass
Asked Answered
S

3

1

I exported my local MongoDB Collections using the JSON file type on my pc. Then I wanted to import these Collections on my root server using MongoDB Compass.

Everytime when I try to export the Collection, it throws the following error: Error

That's how my JSON file looks like:

{..."settings":{"inventory":{"crate":{"$numberInt":"0"},"cratekey":{"$numberInt":"0"},"pickaxe":{"$numberInt":"0"},...}

(I don't know if it's relevant to answer this question but this JSON line is just 1/142000)

How can I fix this error?

Sommersommers answered 17/3, 2019 at 10:55 Comment(0)
T
3

TL;DR
You need to have 1 empty line at the very bottom of the json file.

Long version
I don't know if this will help your case, but I ran into a similar issue when trying to import json data. I had 1 document per line, but something was still wrong. I then exported a similar piece of data as json, and tried playing around with it to see what was causing the issue. It turns out that the json must have an empty line at the bottom. So let's say you have 1 document to import. You place the entire document onto the first line. Then you just hit enter and create the second line at the bottom. After this, my data was imported without a problem.

Tachyphylaxis answered 17/7, 2019 at 15:42 Comment(5)
There is a "remove white space" button on this link that will minify it for you: jsonviewer.stack.huTell
@GaryVaughan The issue isn't related to whitespace or minifying the json. The issue was a missing empty line at the end of the json fileTachyphylaxis
That sounds strange to me that you need a second line at the bottom of the file. But I guess if your json looks like: {..."settings":{"inventory":{"crate":{"$numberInt":"0"},"cratekey":{"$numberInt":"0"},"pickaxe":{"$numberInt":"0"},...} , you probably do? Please enlighten me on this as I wonder why I've never had that problem with my imports, nor have ever heard of this.Tell
@GaryVaughan I totally agree that this is very strange behaviour, but this just seems to be the internal implementation of MongoDB Compass. An empty line was present when exporting the data as well, so when I removed it and imported it back in, I got that error. Note, this was answered a year ago, and there have been updates to MongoDB Compass, so that could have changed with newer versions. I didn't have time to check if this has been changed.Tachyphylaxis
You know, I never checked with Compass. I usually did an export and immediately imported it into another collection on another db. Interesting...Tell
B
0

So the problem is you need to minify your json document. That is it should be in one line. So here's link to website where you will paste your json document on the left and get the minified document on the right. It worked for me. I hope this also helps you. https://codebeautify.org/jsonminifier

Bell answered 2/7, 2020 at 14:27 Comment(0)
D
-1

I had exact same problem, apparently their should be one document per line. However use https://studio3t.com/ it'll work fine.

Delative answered 4/4, 2019 at 12:56 Comment(1)
Its not a solution to the issueMureil

© 2022 - 2024 — McMap. All rights reserved.