Docs Menu
Docs Home
/
MongoDB Kafka Connector
/ /

JSON Formatters

On this page

  • Overview
  • JSON Formatters
  • Built-In Formatters
  • Examples
  • Default JSON Formatter
  • Extended JSON Formatter
  • Simplified JSON Formatter

In this guide, you can learn how to specify built-in JSON formatter classes to use in a MongoDB Kafka source connector.

JSON formatters specify how JSON data appears. They provide instructions for how the connector represents different types when it outputs data.

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.

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
"5f15aab12435743f9bd126a4"
ObjectID ($oid)
w
[ 12345.6789, 23.53 ]
Array of:
- Decimal128 ($numberDecimal)
- Double ($numberDouble)
x
"SSBsb3ZlIGZvcm1hdHRpbmch" of type "00"
Binary ($binary)
y
"2023-05-11T08:27:07.000Z"
Date ($date)
z
{ a: false, b: 87, c: "hello world" }
Document with fields:
- a: Boolean
- b: Int32 ($numberInt)
- c: String

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 type

  • Decimal value with its BSON type

  • Binary value with its buffer string and binary type

  • Date 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"}
}

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 type

  • Decimal value with its BSON type

  • Double value with its BSON type

  • Binary value with its buffer string and binary type

  • Date value as milliseconds since the UNIX epoch

  • Int32 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"}
}

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 string

  • Decimal value as a string

  • Binary value as its buffer string

  • Date 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"}
}

Back

Apply Schemas