liveBook · Manning

JSON Schema

As explained in Kafka Streams in Action, Second Edition

JSON Schema is a vocabulary and specification for describing and validating the structure and constraints of JSON data.

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

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains object properties, required fields, defaults, references, generation, and serializer configuration; defers full JSON Schema specification details.
Examples
Uses Avenger objects, a Company executives array referencing person.json, and a typed SimpleAvengerJson consumer configuration.
What it assumes
Introduces JSON Schema through JSON schema files while assuming a running Schema Registry and Java/Kafka client configuration.
What you can do after
Apply it via Schema Registry and Kafka JSON Schema serializers to exchange typed or generic Java JSON records.
How it connects
Connects JSON Schema with Schema Registry, Kafka producers and consumers, serializers, deserializers, Gradle, and schema references.

Also explained in

Compare how 4 books explain JSON Schema →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.