JSON Formatters
On this page
Overview
In this guide, you can learn how to specify built-in JSON formatter classes to use in a MongoDB Kafka source connector.
JSON Formatters
JSON formatters specify how JSON data appears. They provide instructions for how the connector represents different types when it outputs data.
Built-In Formatters
The following table describes the formatter classes available in the source connector:
Class | Description |
---|---|
com.mongodb.kafka.connect.source.json.formatter.DefaultJson | The legacy strict JSON formatter. |
com.mongodb.kafka.connect.source.json.formatter.ExtendedJson | The fully type-safe extended JSON formatter. This formatter
emphasizes type preservation and represents most values with
their BSON type. |
com.mongodb.kafka.connect.source.json.formatter.SimplifiedJson | The simplified JSON formatter. This formatter represents
ObjectId , Decimal , Date , and Binary values as
strings. |
Examples
The following table describes the fields of the sample document that the examples in this guide use to demonstrate each output format. The columns describe the field name, value, and type for each field or nested field.
Name | Value | Type | |
---|---|---|---|
_id |
| ObjectID ($oid ) | |
w |
| Array of: - Decimal128 ($numberDecimal )- Double ($numberDouble ) | |
x |
| Binary ($binary ) | |
y |
| Date ($date ) | |
z |
| Document with fields: - a : Boolean - b : Int32 ($numberInt )- c : String |
Default JSON Formatter
In the configuration properties for your source connector, set the following property to specify the default JSON formatter:
"output.json.formatter": "com.mongodb.kafka.connect.source.json.formatter.DefaultJson"
When you output the sample document contents from your Kafka topic, the output shows the following type representations:
ObjectId
value with its BSON typeDecimal
value with its BSON typeBinary
value with its buffer string and binary typeDate
value as milliseconds since the UNIX epoch
{ "_id": {"$oid": "5f15aab12435743f9bd126a4"}, "w": [{"$numberDecimal": "12345.6789"}, 23.53], "x": {"$binary": "SSBsb3ZlIGZvcm1hdHRpbmch", "$type": "00"}, "y": {"$date": 1683793627000}, "z": {"a": false, "b": 87, "c": "hello world"} }
Extended JSON Formatter
In the configuration properties for your source connector, set the following property to specify the extended JSON formatter:
"output.json.formatter": "com.mongodb.kafka.connect.source.json.formatter.ExtendedJson"
When you output the sample document contents from your Kafka topic, the output shows the following type representations:
ObjectId
value with its BSON typeDecimal
value with its BSON typeDouble
value with its BSON typeBinary
value with its buffer string and binary typeDate
value as milliseconds since the UNIX epochInt32
value with its BSON type
{ "_id": {"$oid": "5f15aab12435743f9bd126a4"}, "w": [{"$numberDecimal": "12345.6789"}, {"$numberDouble": "23.53"}], "x": {"$binary": "SSBsb3ZlIGZvcm1hdHRpbmch", "$type": "00"}, "y": {"$date": 1683793627000}, "z": {"a": false, "b": {"$numberInt": "87"}, "c": "hello world"} }
Simplified JSON Formatter
In the configuration properties for your source connector, set the following property to specify the simplified JSON formatter:
"output.json.formatter": "com.mongodb.kafka.connect.source.json.formatter.SimplifiedJson"
When you output the sample document contents from your Kafka topic, the output shows the following type representations:
ObjectId
value as its hexadecimal stringDecimal
value as a stringBinary
value as its buffer stringDate
value as a string
{ "_id": "5f15aab12435743f9bd126a4", "w": ["12345.6789", 23.53], "x": "SSBsb3ZlIGZvcm1hdHRpbmch", "y": "2023-05-11T08:27:07Z", "z": {"a": false, "b": 87, "c": "hello world"} }