Creating BSON object from JSON string

JavaJsonMongodbBson

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

... 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:

Solution 2 - Java

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: https://mongodb.github.io/mongo-java-driver/

Solution 3 - Java

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

Solution 4 - Java

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.

Solution 5 - Java

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();

Solution 6 - Java

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).

Solution 7 - Java

You'll find the answer to your question in the source code of https://github.com/mongodb/mongo/blob/master/src/mongo/db/jsobj.cpp Which has the BSON to JSON conversion.

Basically, stuff like

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

and so on...

Solution 8 - Java

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();
}

Solution 9 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaciek SawickiView Question on Stackoverflow
Solution 1 - JavayairView Answer on Stackoverflow
Solution 2 - JavaeskatosView Answer on Stackoverflow
Solution 3 - JavaLakindu AkashView Answer on Stackoverflow
Solution 4 - JavaHank GayView Answer on Stackoverflow
Solution 5 - JavaLeticia SantosView Answer on Stackoverflow
Solution 6 - JavaStaxManView Answer on Stackoverflow
Solution 7 - JavaKresten Krab ThorupView Answer on Stackoverflow
Solution 8 - JavahunterinoView Answer on Stackoverflow
Solution 9 - Javamayank_guptaView Answer on Stackoverflow