JSON Schema is a JSON-based vocabulary for describing JSON data consistently and validating its shape. In event streaming, it defines the contract for records exchanged by producers and consumers and is one of the formats supported by Schema Registry alongside Avro and Protocol Buffers. section 3.2.5 section 3.9
A schema describes an object through its type and properties. Each property declares its own type; arrays also declare item types. Required fields make the contract explicit, while a default can supply an empty array for an omitted collection. Unlike an Avro schema, JSON Schema places object fields in properties rather than a fields array. section 3.2.5
Listing 3.4 JSON Schema schema for Avengers
{
"$schema": "http://json-schema.org/draft-07/schema#", #1
"title": "Avenger",
"description": "A JSON schema of Avenger object",
"type": "object", #2
"javaType": "bbejeck.chapter_3.json.SimpleAvengerJson", #3
"properties": { #4
"name": {
"type": "string"
},
"realName": {
"type": "string"
},
"movies": {
"type": "array",
"items": {
"type": "string"
},
"default": [] #5
}
},
"required": [
"name",
"realName"
]
}
The Avenger schema declares an object with required name and realName strings plus a movies array of strings. Its $schema value identifies draft-07; javaType associates the schema with a Java class used by the application. section 3.2.5
Use JSON Schema in a Kafka application as a repeatable workflow:
- Keep schema files in source control and generate Java domain objects with an open-source generator such as js2p-gradle, reducing repetitive boilerplate and type mismatches. section 3.2.6
- Register schemas with Schema Registry, which stores schemas, provides versioning and compatibility checks, and exposes REST operations for uploading, viewing, and testing compatibility. section 3.9
- Configure KafkaJsonSchemaSerializer and KafkaJsonSchemaDeserializer with the Schema Registry URL. Set JSON_VALUE_TYPE when consumers should receive a specific Java class; otherwise deserialization returns a Map. section 3.7.3
Reuse shared definitions with $ref instead of repeating nested objects. A company schema can define an executives array whose items refer to person.json. When using Schema Registry references, register the referenced schema first; references must use the same schema type. section 3.5
Consume a typed Avenger record
For a JSON Schema-backed Avenger event, configure a String key serializer and KafkaJsonSchemaSerializer for values. On the consumer, use KafkaJsonSchemaDeserializer and set JSON_VALUE_TYPE to SimpleAvengerJson so the serialized record becomes that class rather than a generic Map. section 3.7.3



