JSON Utilities
Author:
Kirill Gaiduk
Changed on:
28 July 2025
Overview
The `JsonUtils`
class in the `util-core`
bundle provides a foundational set of helper methods for working with JSON data. Built on top of the Jackson library, these utilities simplify the conversion of raw JSON into usable Plain Old Java Objects (POJOs) and vice-versa.
JSON is a common data format found in event attributes, entity attributes, and setting values, and these utilities are essential for effective rule development.
Key points
- Universal Conversion (
`anyToPojo`
):This method can convert almost any Java object (especially`Map`
s from event attributes) into a strongly-typed POJO, making the code safer and easier to read. - POJO to Map (
`pojoToMap`
): The reverse of`anyToPojo`
. This is essential for when it is necessary to need to add a complex object as an attribute to a new event. - String to POJO (
`stringToPojo`
): Directly converts a raw JSON string into a typed POJO, which is useful when dealing with data from external systems or settings. - Built on Jackson: The utilities are built on the Jackson library.
Core Methods
`anyToPojo`
This is one of the most frequently used methods in the entire utility library. It can convert almost any Java `Object`
into a typed Plain Old Java Object (POJO). Use this method for converting unstructured `Map`
objects from event attributes or settings into strongly-typed objects that are easier and safer to work with.
1// Attribute value might be a map like this:
2// { "productRef": "SKU-123", "quantity": 10 }
3
4// Define a POJO to represent this data structure
5@Data // from lombok
6public class OrderItemData {
7 private String productRef;
8 private int quantity;
9}
10
11// In your rule, get the attribute from the event
12Object itemDataAttribute = context.getEvent().getAttributes().get("itemData");
13
14// Use anyToPojo to convert the map to your typed object
15OrderItemData itemData = JsonUtils.anyToPojo(itemDataAttribute, OrderItemData.class);
16
17// Now you can work with the typed object
18String productRef = itemData.getProductRef();
`pojoToMap`
This method does the reverse of `anyToPojo`
. It converts a Java object (POJO) into a `Map<String, Object>`
, which is useful when you need to add complex data as an attribute to an event.
1MyCustomObject data = new MyCustomObject("value1", 123);
2
3// Convert the POJO to a map
4Map<String, Object> dataAsMap = JsonUtils.pojoToMap(data);
5
6// Add the map as an event attribute
7EventUtils.forwardInlineEventWithAttributes(context, "EVENT_WITH_DATA", dataAsMap);
`stringToNode`
/ `stringToPojo`
These methods handle the conversion of a JSON string into either a generic `JsonNode`
(for manual tree traversal) or directly into a typed POJO.
1String jsonString = "{\"key\":\"value\"}";
2
3// Convert string to a generic JsonNode
4JsonNode node = JsonUtils.stringToNode(jsonString);
5String value = node.get("key").asText();
6
7// Convert string directly to a POJO
8MyPojo pojo = JsonUtils.stringToPojo(jsonString, MyPojo.class);
`objectToNode`
This method converts any object into its `ObjectNode`
representation, which can be useful for more complex JSON manipulation.
1MyPojo pojo = new MyPojo("data");
2ObjectNode node = JsonUtils.objectToNode(pojo);
3node.put("newField", "newValue");