Chapter 14. Event transformation
EventTransform is a Knative API resource that enables declarative transformations of HTTP requests and responses without requiring custom code. With EventTransform, you can:
- Modify the event attributes
- Extract data from the event payloads
- Reshape events to meet the requirements of different systems
EventTransform is designed as a flexible component in an event-driven architecture. You can place it at various points in the event flow to support seamless integration between diverse producers and consumers.
EventTransform is an addressable resource and can be referenced from Knative sources, triggers, and subscriptions.
14.1. Key features of event transformation Copy linkLink copied to clipboard!
You can use the EventTransform resource to simplify event manipulation, improve flexibility, and enable seamless integration across systems.
| Feature | Description |
|---|---|
| Declarative transformations | Define event transformations using standard Kubernetes resources without writing custom code. |
| JSONata expressions | Use JSONata to extract fields, reshape data, and perform complex transformations. |
| Addressable resource |
Reference |
| Flexible deployment |
Place |
| Sink configuration | Route transformed events to specific destinations by defining a sink. |
| Reply support | Transform and republish events using a built-in reply feature of Broker. |
14.2. Common use cases for event transformation Copy linkLink copied to clipboard!
EventTransform enables you to manipulate and reshape events in different ways depending on system requirements. The most common use cases are as follows:
14.2.1. Field extraction Copy linkLink copied to clipboard!
You can extract specific fields from event payloads and expose them as CloudEvent attributes. This makes it easier to filter and route events downstream.
Example of extracting user ID from an event payload
14.2.2. Event format conversion Copy linkLink copied to clipboard!
You can convert the structure of an event into a different format so that it remains compatible with diverse consumer systems.
Example of converting an order event to a customer-centric format
14.2.3. Event enrichment Copy linkLink copied to clipboard!
You can add fixed or dynamic metadata such as environment or region to events without requiring any changes in the original producer.
Example of adding environment and region metadata to the event
14.2.4. Event response reply transformation Copy linkLink copied to clipboard!
You can transform not only the request sent to a sink but also the response received from the sink, enabling end-to-end event shaping.
Example of transforming both request and reply messages
14.3. Deployment patterns for event transformation Copy linkLink copied to clipboard!
You can use EventTransform at different points in your event flow depending on your architecture needs. The following patterns are supported:
14.3.1. Source to Broker event transformation Copy linkLink copied to clipboard!
You can route events from a Source to an EventTransform resource, apply transformation logic, and then forward them to a Broker so that only normalised or enriched events are routed for consumption.
You can configure an ApiServerSource resource to send events to an EventTransform resource, which then transforms and routes them to the default Broker, as shown in the following example:
14.3.2. Trigger to Service event transformation Copy linkLink copied to clipboard!
You can route events through Broker EventTransform resource reshapes or enriches the filtered events, and the Service or Sink processes the results.
You can tailor events for specific consumers without changing the original producer or affecting other subscribers. Because transformations apply only after filtering, only relevant events are reshaped, which improves efficiency and reduces unnecessary processing.
You can filter events of type original.event.type, route them to an EventTransform, and deliver the transformed events to a Service with this configuration.
14.3.3. Broker reply event transformation Copy linkLink copied to clipboard!
You can configure EventTransform without a sink to republish transformed events back to the Broker, where they can be routed to additional Triggers or consumers.
When using the Broker reply feature, ensure the transformed events do not match the same Trigger that invoked the EventTransform. Otherwise, you risk creating an infinite event loop.
You can filter events of type original.event.type with a Trigger, transform them with EventTransform, and republish them to the Broker as transformed.event.type type. Updating the type or another attribute routes the event to different Triggers without reprocessing by the same Trigger.
14.4. Event transformations for JSON with JSONata Copy linkLink copied to clipboard!
JSONata is a lightweight query and transformation language for JSON data. Within Knative EventTransform, JSONata expressions provide an important way to reshape events. You can use them to extract values from event data, promote specific fields to CloudEvent attributes, restructure entire payloads, add computed values, and even apply conditional logic to control how events are transformed.
You can specify a JSONata expression in the spec.jsonata.expression field of the EventTransform resource. The expression maps the incoming CloudEvent into a new CloudEvent as shown in the following example:
Example of simple event transformation using JSONata
14.4.1. CloudEvent structure in JSONata Copy linkLink copied to clipboard!
When you use JSONata in an EventTransform, the input to the expression is the full CloudEvent object, which includes all standard attributes and the event payload.
The output of your JSONata expression must produce a valid CloudEvent. At a minimum, the following fields are required to ensure compliance with the CloudEvents specification:
| Field | Requirement | Description |
|---|---|---|
|
|
Must be | Identifies the CloudEvents specification version. |
|
| Unique string | Serves as the unique identifier for the event. |
|
| Required |
Indicates the event type, such as |
|
| Required | Identifies the context in which the event occurred. |
|
| Required | Contains the event payload being delivered. |
By explicitly constructing these fields in your JSONata expression, you ensure that the transformed event is valid while still applying reshaping, enrichment, or conditional logic as needed.
14.5. Common transformation patterns Copy linkLink copied to clipboard!
To effectively shape your event data, you can leverage common JSONata transformation patterns for preserving, extracting, restructuring, or conditionally modifying events.
14.5.1. Preserving the original event structure Copy linkLink copied to clipboard!
You can add or adjust attributes while keeping the rest of the event unchanged, so that downstream consumers receive the original data with minimal modifications.
Example of adding a static attribute while preserving the original event structure
14.5.2. Extracting fields as attributes Copy linkLink copied to clipboard!
You can promote values from the payload to top-level CloudEvent attributes to make filtering and routing easier.
Example of extracting user ID and region from the payload and expose them as attributes
In JSONata, the $ symbol represents the entire input object. Using data: $ preserves the original event payload while promoting selected fields.
14.5.3. Restructuring an event data Copy linkLink copied to clipboard!
Use JSONata to transform events into the schema required by another system by restructuring payloads, renaming fields, nesting objects, and performing calculations.
Example of restructuring an order event and calculate the total value of items
Given the transformation above, and this JSON object as input:
The transformation produces the following output:
Use this pattern to integrate with APIs that require specific structures or calculated fields.
14.5.4. Conditional transformations Copy linkLink copied to clipboard!
With JSONata, you can embed conditional logic directly into your transformation, enabling dynamic event shaping based on attributes or payload values.
Example of applying different types and priorities based on conditions
In the given example:
-
If the event type is
order.created, the new type becomesnew.order; otherwise, it is set toupdated.order. -
If the total field in the payload is greater than 1000, a
priorityattribute is added with the valuehigh; otherwise, it is set tonormal.
14.6. Advanced JSONata features Copy linkLink copied to clipboard!
You can use JSONata to transform events more effectively by applying advanced features such as array processing and built-in functions.
14.6.1. Array processing Copy linkLink copied to clipboard!
JSONata makes it easy to work with arrays in event payloads. You can count items, filter them based on conditions, and calculate aggregate values.
Example of processing items in an order and compute totals
Given this as an input:
The transformation produces the following output:
In this example:
-
$count(order.items)counts how many items are in the array. -
The filter
[quantity > 1]selects only items with quantity greater than one. -
$sum(order.items.(price * quantity))calculates the total value.
14.6.2. Using built-in functions Copy linkLink copied to clipboard!
JSONata includes a wide range of built-in functions that can manipulate strings, numbers, dates, and arrays. These functions can enrich events with new fields derived from existing data.
Example of adding metadata using built-in functions
In this example:
-
$now()adds the current timestamp. -
$lowercase()converts the user name to lowercase. -
$split()breaks the name into parts,$map()extracts the first letter of each, and$join()combines them into initials.
14.7. Transforming replies Copy linkLink copied to clipboard!
You can configure an EventTransform resource to modify both incoming requests and the responses returned from a sink in request–reply scenarios.
You must use the same type of transformation for both the request and the reply. For example, JSONata with JSONata.
The following example shows how to transform both request and reply events:
14.8. Event transformation examples Copy linkLink copied to clipboard!
You can use these examples to apply JSONata in EventTransform resources to reshape and enrich events for common business scenarios.
14.8.1. User registration event transformer Copy linkLink copied to clipboard!
You can use the following example to process user registration events by normalising email addresses, deriving a display name if missing, setting a registration timestamp, and applying default values for region and subscription tier.
14.8.2. Order processing event transformer Copy linkLink copied to clipboard!
The following example processes order events. It calculates totals, tax, and grand total, determines the order priority, and enriches the event with processing timestamps: