23

While creating an application in which data is stored as a JSON file, I need it to be imported into MongoDB. I tried to use the following command but got the error --> Failure parsing josn near: } my command is:

mongoimport -d mydb -c mydb --type json --file glossary.json --headerline

My file is located in C:\mongodb\bin\glossary.json.
Here is the content of my file:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
    }

2 Answers 2

29

Your JSON seems to have only a single object. Format like {..},{..} is expected.

So, use --jsonArray option:

 mongoimport -d mydb -c mycollection --jsonArray < glossary.json

The other option is to format the source document as mongodb expects it to be. This will make loading much faster.

Sign up to request clarification or add additional context in comments.

3 Comments

@user2511142 I used --jsonArray option, it worked for your file. Updated in answer.
can you please tell me what is the way to see imported json file's data ?
@user2511142 open mongo shell, run use mydb, then db.mycollection.find().pretty().
6

If you have a large collection, --jsonArray takes a very long time to import. I was getting about 10 documents/second. If you have a properly formatted .json file you can just use --file instead. I'm getting about 6000 documents/second now.

Good thing I didn't settle for the first option!

3 Comments

what is the "properly formatted .json file"? Can you reference or show an example, please?
this has been quite a while, but I think I was just referencing something that is valid json - so maybe try pasting your json text somewhere like here: jsonlint.com to verify its correct
The file may need to be formatted with a single json object per line (as in, all of it on the same line, not spread over several lines). I have also seen some comments that there can be no white space. So this is well formatted: {"foo":"bar"} But this is not: { "foo" : "bat" }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.