Importing JSON file using mongimport, keep getting `unexpected identifier`?
Asked Answered
S

2

29

I'm trying to add a JSON file to mongodb using mongoimports from terminal, here:

 mongoimport --db my_db --collection my_collection --file /content/2_read.json

I keep getting JavaScript execution failed: SyntaxError: Unexpected identifier I ran my JSON through JSON Lint: http://jsonlint.com/ which says it's valid JSON.

I'm not sure what could be tripping up the import process?? Or how to investigate further to hunt down the issue?

UPDATE

  1. Somebody suggested putting all on one-line. A decent suggestion. I tried it and it didn't work.
  2. I tried importing a very very simple json file named simple.json with the content {'content' : 'simple'} But I'm still getting the same error using mongoimport. (I can add documents from mongo shell just fine.)

A sample of the JSON is below.

2_read.json

{
  "name" : "John",
  "tasks" : [
      {
       "ix" : "1",
       "description" : "description of task",
       "tags": []
      }, 
     {
      "ix": "2",
      "description" : "description of task",
      "tags" : []
     }
       ]
}

Thanks.

Superfluous answered 11/5, 2013 at 17:54 Comment(3)
possible duplicate of Error during mongoimportFaille
Maybe? But the person got a specific error, when the error I'm getting is generic (and less helpful). I also just tried adding --jsonArray but that didn't change anything. ;(Superfluous
I did a complete newbie error. I thought you used mongoimports from the mongo shell prompt instead of the main terminal prompt. After that, I ran into the error of needing --jsonArray (and I got specific errors like in the post your pointed too) Thanks for highlighting that.Superfluous
S
126

Answering my own question because complete newbies will appreciate the error and may run into it as they get started.

mongoimport is used from terminal, NOT within the mongo shell. Just like you don't use npm inside node. ;D

After figuring out my conceptual error there, I needed --jsonArray as pointed to by @WiredPrairie in the comments.

Superfluous answered 11/5, 2013 at 19:14 Comment(4)
Even I did the same mistake. Thanks for sharing. It saved my timeThames
Just made the same mistake. Thanks for answering your question.Truss
Check the documents for erroneous text. I copied the json document from the web, and the browser through an error that was copied, when I copied the content of the page. Hopefully this helps you. JSLint helps too!Henson
so what do you use inside mongo shell instead?Jericajericho
A
0

The following command from terminal worked good for me:

mongoimport -d my_db -c my_collection < /content/2_read.json --batchSize 1
Arsis answered 30/11, 2015 at 8:56 Comment(1)
Hi. Im getting ERROR: unrecognised option '--batchSize' with the following command mongoimport --db ciudadDeBuenosAires --collection vehiculos-peajes < "/home/dantebarba/Descargas/ausapasovehiculo-2015.json" --batchSize 1Tella

© 2022 - 2024 — McMap. All rights reserved.