Maciek Sawicki
Maciek Sawicki

Reputation: 6835

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?

Upvotes: 65

Views: 137447

Answers (9)

Lakindu Akash
Lakindu Akash

Reputation: 1036

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

Upvotes: 19

hunterino
hunterino

Reputation: 464

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

Upvotes: 2

Leticia Santos
Leticia Santos

Reputation: 559

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

Upvotes: 14

eskatos
eskatos

Reputation: 4434

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/

Upvotes: 46

yair
yair

Reputation: 9245

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

Upvotes: 57

Kresten Krab Thorup
Kresten Krab Thorup

Reputation: 74

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

Upvotes: 2

mayank_gupta
mayank_gupta

Reputation: 21

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.

Upvotes: 1

StaxMan
StaxMan

Reputation: 116472

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

Upvotes: 3

Hank Gay
Hank Gay

Reputation: 71939

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.

Upvotes: 12

Related Questions