dev777 dev777 - 1 year ago 302
JSON Question

How to convert json Object to Document in mongodb using java

I'm using the below sample json :

JSONObject json=new JSONObject();
json.put("time_range", "22-23");
json.put("flow_id", "786");

And trying to convert to Document as follows :

Document doc = (Document) JSON.parse(jsonlist.toString()); // conversion from json to Document

col.insertOne(doc); // inserting into Mongo collection

I'm facing the below error:

java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to org.bson.Document

Can anyone please help me out regarding this issue ...

Answer Source

Try this

Document doc = Document.parse( jsonlist.toString() );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download