Reputation: 10746
I have json file where each line looks like:
[1, "A", "B", 10, "{\"key\":\"val"}"]
How to import the file using JAVA driver api or other means?
Using the below function :
public static void importJSONFileToDBUsingJavaDriver(String pathToFile, DB db, String collectionName) {
// open file
FileInputStream fstream = null;
try {
fstream = new FileInputStream(pathToFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("file not exist, exiting");
return;
}
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
// read it line by line
String strLine;
DBCollection newColl = db.getCollection(collectionName);
try {
while ((strLine = br.readLine()) != null) {
// convert line by line to BSON
DBObject bson = (DBObject) JSON.parse(JSONstr);
// insert BSONs to database
try {
newColl.insert(bson);
}
catch (MongoException e) {
// duplicate key
e.printStackTrace();
}
}
br.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
resulted in the following error (on line newColl.insert(bson);
):
java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [_id]
at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:161)
at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:152)
at org.bson.types.BasicBSONList.get(BasicBSONList.java:104)
at com.mongodb.DBCollection.apply(DBCollection.java:766)
at com.mongodb.DBCollection.apply(DBCollection.java:755)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:242)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
at com.mongodb.DBCollection.insert(DBCollection.java:75)
at com.mongodb.DBCollection.insert(DBCollection.java:59)
at com.mongodb.DBCollection.insert(DBCollection.java:104)
Trying to use mongoimport.exe utility :
mongoimport --db db --collection dbColl < rows.js
resulted in:
exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
I would be happy if someone can refer me to JSON format specification which is acceptable by MongoDB. (i mean it can be converted to BSON, then DBObject can be built from it and this object can be inserted to MongoDB collection)
Upvotes: 1
Views: 2165
Reputation: 5139
You are trying to create a DBObject from the string, that is actually an array, hence directly, you cant typecast it, using JSON.parse(JSONstr)
. So you have to first convert your string to Object and then insert it.
String JSONstr = "[1, \"A\", \"B\", 10,{\"key\":\"val\"}]";
JSONArray jsonArray = new JSONArray(JSONstr);
JSONObject jsonObject = new JSONObject();
jsonObject.put("data", jsonArray);
DBObject bson = (DBObject) JSON.parse(jsonObject.toString());
WriteResult insert = newColl.insert(bson);
Upvotes: 2