Feb 23, 2021

Jolt custom java transform

Jolt is a JSON to JSON transformation library where the transform is defined in JSON. It’s really good at reorganizing the json data and massaging it into the output JSON you need. Sometimes, you just need something a little more powerful, and Jolt has a way to extend it’s transfomations to do whatever custom logic you need in Java. In this example let’s assume we get sent in just a personId, but we need the whole person object in our output. We can add a custom transformation that will make a REST call and fill in that data.

First, let’s take a look at how we define our transformation in the Jolt JSON. The “operation” here needs to point to the canonical name (package and class) of the Java class that we write. The “spec” defines the configuration of our transformation. In this case we are going to take the “personId” make a call to our “url” and put the object in “person”

Jolt Transform
    "operation" : "transform.LookupTransform",
    "spec" : {
      "personId": {
        "url" : "https://localhost:8080/person/",
        "key": "person"

Next, let’s write the transform. We need to implement the SpecDriven interface so we can get access to the spec, and the Transform interface so that Jolt knows what method to call on the transformation.

In our transform we will look through all the keys of the input object and if the spec has been defined for that key we will grab the vales for “url” and “key”. We use those values to know where to make the REST call and where to put the output. In this example I just hard code a Map for the return value, but making a call to another service here to make the call could be substituted.

Custom Transform
package transform;
import com.bazaarvoice.jolt.SpecDriven;
import com.bazaarvoice.jolt.Transform;
import javax.inject.Inject;
import java.util.Map;
public class LookupTransform implements SpecDriven, Transform {
    private final Object spec;
    public LookupTransform(Object spec) {
        this.spec = spec;
    public Object transform(Object input) {
        Map inputMap = (Map) input;
        Map specMap = (Map) spec;
        for (Object key : specMap.keySet()) {
            if (inputMap.containsKey(key)) {
                Map lookupDef = (Map) specMap.get(key);
                String lookupDefUrl = (String) lookupDef.get("url");
                String lookupDefKey = (String) lookupDef.get("key");
                String inputValue = (String) inputMap.get(key);
                Object lookedUpValue = lookup(lookupDefUrl, inputValue);
                inputMap.put(lookupDefKey, lookedUpValue);
        return input;
    public Object lookup(String url, String value) {
        //Just return a map here, but in a real example make a http call to the url to get the object
        return Map.of(
                "id", "12345",
                "firstName", "John",
                "lastName", "Doe",
                "age", "32"

Finally, with our custom transformation we can turn this input with just a personId into the output with the full person object.

  "personId": "12345"
  "personId": "12345",
  "person": {
    "age": "32",
    "lastName": "Doe",
    "id": "12345",
    "firstName": "John"

You can see my example with tests here https://github.com/scottbock/Jolt

About the Author

Scott Bock profile.

Scott Bock

Principal Technologist

Scott is a Senior Software Engineer with over 12 years of experience using Java, and 5 years experience in technical leadership positions. His strengths include troubleshooting and problem solving abilities, excellent repertoire with customers and management, and verbal and written communication. He develops code across the entire technology stack including database, application, and user interface.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
React Server Components
The React Team recently announced new work they are doing on React Server Components, a new way of rendering React components. The goal is to create smaller bundle sizes, speed up render time, and prevent […]
Page Object Model for UI Testing
How to make a multi-handled range input
The HTML range input is a great way to allow your users to manipulate a numeric value using their mouse. The range input that is currently provided by browsers only supports a single handle, which […]
Test your production UI code without deployment using Chrome DevTools
Have you ever faced an issue in a production, after a release, that you did not encounter during development? Wouldn’t it be helpful to test your hotfixes against the production environment before deployment? The solution […]