Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 991
cathy.sasaki Avatar asked May 11 '13 17:05

cathy.sasaki


People also ask

How do I import data into Mongoimport?

If you have CSV files (or TSV files - they're conceptually the same) to import, use the --type=csv or --type=tsv option to tell mongoimport what format to expect. Also important is to know whether your CSV file has a header row - where the first line doesn't contain data - instead it contains the name for each column.

Can we import JSON in MongoDB?

The process to import JSON into MongoDB depends on the operating system and the programming language you are using. However, the key to importing is to access the MongoDB database and parsing the file that you want to import. You can then go through each document sequentially and insert into MongoDB.

What is Mongoimport?

The mongoimport command is used to import your content from an extended JSON, CSV, or TSV export created by mongoexport.


1 Answers

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.

like image 63
cathy.sasaki Avatar answered Sep 17 '22 18:09

cathy.sasaki