I used a combination of BasicDBObject toString () and GSON library to obtain a printed copy of the JSON:
com.mongodb.DBObject obj = new com.mongodb.BasicDBObject(); obj.put("_id", ObjectId.get()); obj.put("name", "name"); obj.put("code", "code"); obj.put("createdAt", new Date()); com.google.gson.Gson gson = new com.google.gson.GsonBuilder().setPrettyPrinting().create(); System.out.println(gson.toJson(gson.fromJson(obj.toString(), Map.class)));
source share