Kafka Message Encoder
Encode JSON to Avro binary for Kafka and get a ready-to-paste kcat producer command.
- Free
- No sign-up
- Runs in your browser
Kafka Message Encoder: loading the in-browser tool…
No warranty. Unless required by applicable law, this tool is provided on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. You are solely responsible for determining the appropriateness of using it and assume any risks associated with doing so.
How it works
Encode to raw Avro or the Confluent envelope, then copy the bytes or a ready-to-paste kcat command, all in your browser.
Write your JSON message
Enter the record (and an optional key) you want to produce, along with its AVSC schema.
Choose the wire format
Encode as raw Avro, or wrap it in the Confluent Schema-Registry envelope with a schema id. Pick base64 or hex output.
Copy the bytes or kcat command
Grab the encoded bytes, download the binary, or copy a ready-to-paste kcat producer command for your broker and topic.
Your data stays put. Your messages, keys and schema are encoded entirely in your browser. Nothing is uploaded, logged, or sent to any server.
Avro and the Confluent wire format
When do you need to encode an Avro message?
To test a consumer, reproduce a bug or seed a topic, you need messages exactly as a real producer writes them. Writing a producer for one test message is slow, and plain JSON sent to an Avro topic fails in every consumer that expects Avro.
The encoder turns a JSON record and its schema into Avro bytes, with the Schema Registry header if your consumers expect one, ready to copy as base64 or hex, or to download as a file for the producer you test with.
What the encoder writes
Avro binary has no field names and no type tags. Values follow the schema's field order. An int or long is a zigzag varint, a string is its byte length followed by its UTF-8 bytes, and a double is 8 bytes, little-endian. A union is the branch index, then the value. Reading the bytes back takes the schema they were written with.
The Confluent wire format puts a 5-byte header before the Avro body: the magic byte 0x00, then the schema id as a 4-byte big-endian integer. The id is the registry's global id, not the subject version. KafkaAvroDeserializer reads the id, fetches that schema and decodes the body with it.
Pick Schema Registry when consumers use Confluent's deserializer, because it rejects anything without the header. Pick Raw Avro when the consumer already holds the schema.
Key and value are encoded separately. A key with no schema is sent as UTF-8 text, the bytes Kafka's StringSerializer writes, with no header. A key with a schema becomes Avro, and under Schema Registry it gets its own header and id, from the subject <topic>-key under the default naming strategy.
Worked example: an order under Schema Registry
This is the Load example message and schema, with the value schema registered as id 42, a plain string key, and Hex output so the header is visible.
{
"orderId": "A-10473",
"total": 248.5,
"vip": true,
"coupon": null,
"qty": 7
}{
"type": "record",
"name": "Order",
"fields": [
{
"name": "orderId",
"type": "string"
},
{
"name": "total",
"type": "double"
},
{
"name": "vip",
"type": "boolean"
},
{
"name": "coupon",
"type": [
"null",
"string"
],
"default": null
},
{
"name": "qty",
"type": "long"
}
]
}order-A-10473- Wire format
- Schema Registry
- Value schema ID
- 42
- Output
- Hex
000000002a0e412d31303437330000000000106f4001000e6f726465722d412d313034373300 is the magic byte and 0000002a is 42, the schema id. The Avro body follows. 0e is the length 7 as a zigzag varint, then come the 7 bytes of A-10473. 0000000000106f40 is 248.5 as a little-endian double. 01 is true. 00 picks branch 0 of the coupon union, which is null, so no value bytes follow. The last byte, 0e, is 7 for qty. The value is 24 bytes in all.
The key has no schema, so it has no header. It is the 13 UTF-8 bytes of order-A-10473.
Common errors
Encode failed: the message may not match the schema. invalid ["null","string"]: "SAVE10"couponis a union ofnullandstring, and the message gives it a bare string. Avro's JSON encoding wraps a non-null union value in its branch name.Write
"coupon": {"string": "SAVE10"}. Anullstays bare.Encode failed: the message may not match the schema. invalid "long": "7"qtyis along, and the message quotes it.Drop the quotes:
"qty": 7.Encode failed: the message may not match the schema. invalid "double": undefinedThe message leaves out
total, which has no default. The error names only the type, so look for a missing field of that type.Add the field, or give it a default in the schema. A field with a default, such as
coupon, can be left out.Key encode failed: the key may not match the key schema. invalid "string": 10473The key schema is
"string"and the key is10473. With a key schema present, the tool parses the key as JSON, where10473is a number.Quote it as
"10473", or clear the key schema to send the key as plain UTF-8 text.
FAQ
How do I produce Avro messages with kcat?
kcat cannot serialize Avro when it produces. Its -s avro option works only in consumer mode. Encode the message here, save it with the value.bin button and pass the file as an argument: kcat -P -b <broker>:9092 -t <topic> -k order-A-10473 value.bin.
kcat sends a file argument as one message. It splits standard input at every newline byte, and an Avro body can contain 0x0a anywhere.
A string key works with -k, as in the example. An Avro key under Schema Registry starts with the byte 0x00, which no command-line argument can carry, so it needs a producer with an Avro key serializer, such as kafka-avro-console-producer.
What is the magic byte in a Kafka message?
It is the first byte of a message in the Confluent wire format, and it is always 0x00. The next 4 bytes hold the schema id, and the Avro body follows.
Why does my consumer say "Unknown magic byte!"?
KafkaAvroDeserializer throws it when the first byte of a record is not 0x00, so the record was written without the Schema Registry header. Encode it again with Wire format set to Schema Registry. If the key fails, the consumer reads keys as Avro while the producer sent a string key.
Does the encoder register my schema in Schema Registry?
No. The tool makes no network calls. Register the schema with POST /subjects/<topic>-value/versions and enter the id from the response as Value schema ID. For a schema already registered, GET /subjects/<topic>-value/versions/latest returns its id.
Producing to Kafka for real?
TypedFlows produces Avro to Kafka from managed flows.