How to import formatted json file to mongodb?

1.8k views Asked by At

I tried to import formatted json file into mongodb. but always it says that can't insert

{
  "marks": [
    {
      "class": {
        "className": "A"
      },
      "subject": "maths",
      "score": 43,
      "grade": "a"
    },
    {
      "class": {
        "className": "B"
      },
      "subject": "maths",
      "score": 34,
      "grade": "c"
    }
  ]
}

what is the reason for this. I used command mongoimport --db sss --collection bbv --file a.json and the error message is

exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
4

There are 4 answers

1
tpei On

try using

mongoimport -d DATABASE_NAME -c COLLECTION_NAME --file YOUR_JSON_FILE --jsonArray

as the import command instead.

@see: Mongodb Mongoimport too large: Failure parsing errors

0
Salvador Dali On

You problem is that this is not a valid JSON document.

If you have doubts like this, go to JSON editor and paste it. It has an error on the line 9, and this means that you have to put "a" there. The same thing is with grade c

0
Jhonatan On

I had a similar problem e solve this change de locale.

Run this command for change locale dpkg-reconfigure locales

0
Pablo Ezequiel Inchausti On

The json file is ok, but the problem is that to import it to MongoDB it should be in only one document by line. This was already commented by @WiredPrairie in this post:

The file a.json:

{"marks": [{"class": {"className": "A"}, "subject": "maths", "score": 43, "grade": "a"}, {"class": {"className": "B"}, "subject": "maths", "score": 34, "grade": "c"} ] }

Command mongoimport:

$ mongoimport --db sss --collection bbv --file a.json

enter image description here

And the result with Robomongo:

enter image description here