8

I am using MongoDB Driver Java API to convert BSON to JSON. I have test code like this.

String input = "{ \"timestamp\" : 1486064586641 }";
org.bson.Document doc = org.bson.Document.parse(input);
System.out.println("input  = " + input);
System.out.println("output = " + doc.toJson());

The output is:

input  = { "timestamp" : 1486064586641 }
output = { "timestamp" : { "$numberLong" : "1486064586641" } }

Is there an easy way to make the output look like the input?

ytw
  • 1,335
  • 2
  • 20
  • 42

2 Answers2

16

BSON Documnet's toJson method supports only output to MongoDB Extended JSON (STRICT or SHELL format). If you want to have regular JSON, you can use com.mongodb.util.JSON class:

String input = "{ \"timestamp\" : 1486064586641 }";
org.bson.Document doc = org.bson.Document.parse(input);
System.out.println("input  = " + input);
System.out.println("output (SHELL) = " + doc.toJson(new JsonWriterSettings(JsonMode.SHELL)));
System.out.println("output (STRICT) = " + doc.toJson(new JsonWriterSettings(JsonMode.STRICT)));
System.out.println("output (JSON) = " + com.mongodb.util.JSON.serialize(doc));

This will generate following output:

input  = { "timestamp" : 1486064586641 }
output (SHELL) = { "timestamp" : NumberLong("1486064586641") }
output (STRICT) = { "timestamp" : { "$numberLong" : "1486064586641" } }
output (JSON) = { "timestamp" : 1486064586641}
Natalja Olefire
  • 442
  • 5
  • 9
  • I've got problem with ObjectId. What if input = "{ \"timestamp\" : 1486064586641, \"_id\": {\"$oid\": \"58dd09687b4e7f108f9e7a2f\"} }". With JSON.serialize i get this result: { "timestamp" : 1486064586641 , "_id" : { "$oid" : "58dd09687b4e7f108f9e7a2f"}}. Timestamp is ok except _id – Alexander Kondaurov Apr 10 '17 at 16:10
  • Yes, but why is it a problem? JSON does not process $oid in any special way, you can add something like "\"aaa\": {\"bbbb\": \"ccc\"}" instead of $oid - result will be very similar. What do you expect to have? – Natalja Olefire Apr 10 '17 at 16:59
  • Thx Natalja, i've created question: http://stackoverflow.com/questions/43328955/bson-to-json-with-mongo-java-driver – Alexander Kondaurov Apr 10 '17 at 17:02
4

Natalja's answer is excellent, but if you are using the Mongo Java driver 3.8.2 upwards you will notice some deprecation warnings. If you want the output to look like the input you can use RELAXED JsonWriterSettings mode.

Below you can see an example with the possible modes and how the JSON will looks like. There are also some deprecation warnings and alternatives to the deprecated code:

String input = "{ \"timestamp\" : 1486064586641 }";
org.bson.Document doc = org.bson.Document.parse(input);
System.out.println("input  = " + input);
JsonWriterSettings shellMode = JsonWriterSettings.builder().outputMode(JsonMode.SHELL).build();
System.out.println("output (SHELL) = " + doc.toJson(shellMode));
JsonWriterSettings strictMode = JsonWriterSettings.builder().outputMode(JsonMode.STRICT).build();
System.out.println("output (STRICT) = " + doc.toJson(strictMode)); // deprecated - use extended like below
JsonWriterSettings extendedMode = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();
System.out.println("output (EXTENDED) = " + doc.toJson(extendedMode));
JsonWriterSettings relaxed = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build();
System.out.println("output (RELAXED) = " + doc.toJson(relaxed));

System.out.println("output (JSON) = " + com.mongodb.util.JSON.serialize(doc)); // deprecated - use relaxed like above    

Also note that the JsonWriterSettings constructor is deprecated and you can use as an alternative the builder method like e.g:

JsonWriterSettings.builder().outputMode(JsonMode.SHELL).build()
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76