javabsonpentaho-data-integrationpdi

Convert an org.bson.Document to an org.bson.BasicBSONObject in Java?


I have a String in Pentaho Data Integration (PDI) a.k.a. Kettle, in a transformation's UDJC step. So using Java, I need to take that String which contains JSON from MongoDB, and put it into a BasicBSONObject. (I can use import statements but I can't install things as part of a solution.)

I got the String into an org.bson.Document so it seems like the hard part's over, but I still can't figure out how to convert one type of bson to another here, resulting in an object of type org.bson.BasicBSONObject

This errors out on the last line:

import org.bson.Document;
...
String mongoResultString = get(Fields.In, "mongoAsset").getString(r);
Document mongoResultDoc = Document.parse(mongoResultString );
BasicBSONObject mongoResult = (BasicBSONObject) mongoResultDoc;

Tried ecosia, google, and searching Stack Overflow. Thanks.


Solution

  • There is a BasicDBObject#parse method in MongoDB Java Driver library, which is most probably present on the classpath of your application. This method, according to the documentation, parses a string in MongoDB Extended JSON format to a BasicDBObject.

    You can use it directly and skip the Document parsing step.

    BasicBSONObject mongoResult = BasicDBObject.parse(mongoResultString);