Alternate API for overloaded parse methods com.mongodb.util.JSON in Mongo-legacy driver 4.06

Hi Team,

We are using Mongo-java driver .311, would like to migrate to mongo legacy java driver 4.0.6.

In mongo docs mentioned to use BasicDbObject.parse method as alternative to com.mongodb.util.JSON.parse, it is not working as expected. Please suggest other api.

Thanks & Regards,
Venkatakrishna Tolusuri

Hi @Venkatakrishna_Tolus,

Can you be more explicit about what your expectations are, and in what way they are not being met? Sample JSON input would be useful as well.


Hi Jeffrey_Yemin,

Thanks for quick response. Please find the issues.

ex 1: Suppose if we need to parse the text

String testString = “‘text’”
When we use old api(Json.parse) result is text but with new Api (BasicDBObject.parse) it is resulting ‘text’.

ex 2: Suppose if we pass array to parse
Old Api : it is returning List as returning as a result.
Proposed api : It is return array as String.

ex 3: com.mongodb.util.JSON class havin a method Json.parse(“String”, callback). In 4.0 haven’t find any alternative.

Kindly suggest alternatives for above api.

Venkata krishna Tolusuri

OK, I see. The 4.0 driver no longer has a way to parse JSON that does not represent a full JSON object. To use BasicDBObject.parse. you’ll have to wrap those JSON object fragments (e.g. strings, arrays) inside a JSON object and then extract the value. For example:

String jsonFragment = "\"text\"";
String fieldName = "val";
String jsonString = String.format("{%s : %s}", fieldName, jsonFragment);

BasicDBObject res = BasicDBObject.parse(jsonString);
Object value = res.get(fieldName);

will print:



1 Like

Hi Jeffrey_Yemin,

I followed your suggestions, now it is working fine. Can you please provide alternate for the below api call.
ex 3: com.mongodb.util.JSON class havin a method Json.parse(“String”, callback). We haven’t find any alternative in 4.0 version

In our application we are taking query as a string and while parsing we are doing customization to query by above method.

Please kindly provide alternate way for that .

Thanks & Regards,
Venkata krishna Tolusuri

Without seen an example of the callback that you’ve written, it’s hard to say. But I suspect anything you do in the callback can also be done subsequent to parsing by mutating the document that is returned from BasicDBObject.parse.