Bson - How to convert JSON to List <Document> and List <Document> to JSON?

I am using Java Driver 3.0 with MongoDB to send JSON through a webservice.

When I want to convert a Document object (org.bson.Document) to JSON I use obj.toJson()

, and when I want to convert JSON to Document object I use Document.parse(json)

.

However, when I deal with lists of documents (represented in JSON :) [{"field1":1, ...}, {"field1":2, ...}]

, I cannot figure out how to do these transformations.

So far I have come up with these "hacks":

  • From list to JSON: I add a list of documents as the value of a field called "list" in a larger document. I am converting this large document to JSON and removing what I don't need from the resulting string.

    public String toJson(List<Document> docs){
        Document doc = new Document("list", docs);
        String json = doc.toJson();
        return json.substring(json.indexOf(":")+2, json.length()-1);
    }
    
          

  • From JSON to List: I do the opposite by adding this "list" field to JSON, converting it to a document, and getting only the value of that field from the document.

    public static List<Document> toListOfDocuments(String json){
        Document doc = Document.parse("{ \"list\":"+json+"}");
        Object list = doc.get("list");
        if(list instanceof List<?>) {
            return (List<Document>) doc.get("list");
        }
        return null ;
    }
    
          

I also tried using a different JSON serializer (I took Google one), but it doesn't give the same result as the built-in method toJson()

from the Document object, especially for the "_id" field or timestamp.

Is there any clean way to do this?

+3


source to share


2 answers


The package is com.mongodb.util.JSON

"still" not out of date and does a great job with lists DBObject

. You just need to convert a little:

    MongoClient client = new MongoClient(new ServerAddress("192.168.2.4", 27017));

    MongoDatabase db = client.getDatabase("test");

    MongoCollection<Document> collection = db.getCollection("sample");

    MongoCursor<Document> iterator = collection.find().iterator();

    BasicDBList list = new BasicDBList();
    while (iterator.hasNext()) {
        Document doc = iterator.next();
        list.add(doc);
    }
    System.out.println(JSON.serialize(list));

      



And there is nothing wrong with adding this "list" to another DBObject

with the key "list" that is used in your release. Otherwise, you can delve into using a different JSON parser and feed each document from the cursor iterator into that.

It depends on the size of your input, but while it still works, it certainly looks much cleaner in code.

+5


source


There is a solution for Driver 3.0.

You do the following:



BasicDBObject dbObject = (BasicDBObject) JSON.parse("yourJsonString");
MongoCollection<BasicDBObject> table = db.getCollection("collectionName", BasicDBObject.class);
table.insertOne(dbObject);

      

+1


source







All Articles