Creating BSON object from JSON string

I have Java app that takes data from external app. Incoming JSONs are in Strings. I would like to parse that Strings and create BSON objects.

Unfortunate I can't find API for that in Java's BSON implementation.

Do I have use external parser for that like GSON?


The easiest way seems to be to use a JSON library to parse the JSON strings into a Map and then use the putAll method to put those values into a BSONObject.

This answer shows how to use Jackson to parse a JSON string into a Map.

Official MongoDB Java Driver comes with utility methods for parsing JSON to BSON and serializing BSON to JSON.

import com.mongodb.DBObject;
import com.mongodb.util.JSON;

DBObject dbObj = ... ;
String json = JSON.serialize( dbObj );
DBObject bson = ( DBObject ) JSON.parse( json );

The driver can be found here:

... And, since 3.0.0, you can:

import org.bson.Document;

final Document doc = new Document("myKey", "myValue");
final String jsonString = doc.toJson();
final Document doc = Document.parse(jsonString);

Official docs:

To convert a string json to bson, do:

import org.bson.BasicBSONEncoder;
import org.bson.BSONObject;

BSONObject bson = (BSONObject)com.mongodb.util.JSON.parse(string_json);
BasicBSONEncoder encoder = new BasicBSONEncoder();
byte[] bson_byte = encoder.encode(bson);

To convert a bson to json, do:

import org.bson.BasicBSONDecoder;
import org.bson.BSONObject;

BasicBSONDecoder decoder = new BasicBSONDecoder();
BSONObject bsonObject = decoder.readObject(out);
String json_string = bsonObject.toString();

You might be interested in bson4jackson project, which allows you to use Jackson data binding to work with BSON (create POJOs from BSON, write as BSON) -- especially since Jackson also work with JSON. So it will allow conversion like you mention, just use different ObjectMapper instanstaces (one that works with JSON, other with BSON).

With Jackson you can either work with full POJOs (declare structure you want) or with simple Maps, Lists and so on. You just need to declare what to type to bind to when reading data (when writing, type is defined by object you pass).

You'll find the answer to your question in the source code of Which has the BSON to JSON conversion.

Basically, stuff like

  • ObjectId("XXX") -> { "$oid" : "XXX" }
  • /XXX/gi -> { "$regex" : "XXX", "$options" : "gi" }

and so on...

I am not sure about java but the mongoDB CPP driver has a function type

BSONObj fromjson(string)

which returns a BSONObj according to the string passed. There should be a same function in Java too.

I would suggest using the toJson() and parse(String) methods of the BasicDBObject, because the JSON utility class has been @Depricated.

import com.mongodb.BasicDBObject;

public static BasicDBObject makeBsonObject(String json) {
    return BasicDBObject.parse(json);

public static String makeJsonObject(BasicDBObject dbObj) {
    return dbObj.toJson();

Use Document.parse(String json) from org.bson.Document. It returns Document object which is type of Bson.

Need Your Help

Error handling in C code

c error-handling

What do you consider "best practice" when it comes to error handling errors in a consistent way in a C library.

Disable and enable buttons in C#


I am working on something fairly simple, well I thought it would be.