For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Work with Extended JSON Data

You can read an Extended JSON string into Scala objects by using the Scala driver's document classes or by using the BSON library directly. The following sections show how to use each approach.

You can read an Extended JSON string into a Scala document object by calling the parse() static method on BsonDocument and then wrapping the result in a Scala Document instance. This approach parses the Extended JSON string in any of the formats and returns a Document containing the data.

The following example shows how you can read an Extended JSON string into a Document object by calling BsonDocument.parse() and passing the result to the Document factory:

val ejsonStr = """{"_id": {"$oid": "507f1f77bcf86cd799439011"}, "myNumber": {"$numberLong": "4794261"}}"""
val document = Document(BsonDocument.parse(ejsonStr))
println(document)

To learn more about documents in MongoDB, see Documents in the MongoDB Server manual.

You can also read an Extended JSON string into Scala objects without using the Scala driver's document classes by using the JsonReader class. This class contains methods to sequentially parse the fields and values in any format of the Extended JSON string, and returns them as Scala objects. The driver's document classes also use this class to parse Extended JSON.

The following code example shows how you can use the JsonReader class to convert an Extended JSON string into Scala objects:

val ejsonStr = """{"_id": {"$oid": "507f1f77bcf86cd799439011"}, "myNumber": {"$numberLong": "4794261"}}"""
val reader = new JsonReader(ejsonStr)
reader.readStartDocument()
val id = reader.readObjectId("_id")
val myNumber = reader.readInt64("myNumber")
reader.readEndDocument()
println(s"$id is type: ${id.getClass.getName}")
println(s"$myNumber is type: ${myNumber.getClass.getName}")

For more information, see the JsonReader API documentation.

You can write an Extended JSON string from your data by using the Scala driver's document classes or by using the BSON library directly. The following sections show how to use each approach.

You can write an Extended JSON string from an instance of Document or BsonDocument by calling the toJson() method. By default, the toJson() method outputs the string in the Relaxed mode format. To use a different format, pass an instance of the JsonWriterSettings class to the toJson() method.

The following example calls the toJson() method without arguments to output the Extended JSON in the default Relaxed mode format:

val document = Document(
"_id" -> BsonObjectId(new ObjectId("507f1f77bcf86cd799439012")),
"myNumber" -> BsonInt64(11223344L)
)
val ejsonStr = document.toJson()
println(ejsonStr)

You can also output an Extended JSON string from data in Scala objects using the BSON library with the JsonWriter class. To construct an instance of JsonWriter, pass a subclass of a Java Writer to specify how you want to output the Extended JSON. You can optionally pass a JsonWriterSettings instance to specify options such as the Extended JSON format. By default, the JsonWriter uses the Relaxed mode format. The Scala driver's document classes also use this class to convert BSON to Extended JSON.

The following code example shows how you can use JsonWriter to create an Extended JSON string and output it to System.out. The example specifies the format by passing the outputMode() builder method the JsonMode.EXTENDED constant:

val writer = new StringWriter()
val jsonWriter = new JsonWriter(writer,
JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build())
jsonWriter.writeStartDocument()
jsonWriter.writeObjectId("_id", new ObjectId("507f1f77bcf86cd799439012"))
jsonWriter.writeInt64("myNumber", 11223344L)
jsonWriter.writeEndDocument()
println(writer.toString())

For more information about the methods and classes mentioned in this section, see the following API documentation:

In addition to specifying the outputMode() to format the JSON output, you can further customize the output by adding converters to your JsonWriterSettings.Builder instance. These converter methods detect specific BSON types and execute the logic defined by the Converter passed to them.

The following sample code shows how to append converters, defined as lambda expressions, to simplify the Relaxed mode JSON output:

val settings = JsonWriterSettings.builder()
.outputMode(JsonMode.RELAXED)
.objectIdConverter((value, writer) => writer.writeString(value.toHexString))
.timestampConverter((value, writer) => {
val instant = Instant.ofEpochSecond(value.getTime.toLong)
writer.writeString(
DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneOffset.UTC).format(instant))
})
.build()
val document = Document(
"_id" -> BsonObjectId(new ObjectId("507f1f77bcf86cd799439012")),
"createdAt" -> new BsonTimestamp(1601516589, 1),
"myNumber" -> BsonInt64(4794261L)
)
println(document.toJson(settings))

For more information about the methods and classes mentioned in this section, see the following API documentation: