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
Rockstar Development
This originally appeared on Marty Henderson’s personal blog Or, how to use Gitpod and GitLab so that no one else has to care about your questionable coding language choices. A true rockstar has a good […]
Testing a Quarkus Kafka Application
Quarkus, a “Kubernetes Native Java stack,” enables lighter Java applications with faster startup times. In a recent post, I talked about scaling Kafka consumers in Kubernetes. Quarkus applications fit right into this picture because they […]
Gitpod and Hringvegurinn
Iceland Ever seen an advertisement for visiting Iceland? Have you noticed that they all mention Hringvegurinn or the Ring Road, as a good tour? (If you haven’t seen a tour ad for Iceland, Steindi Jr […]
Tacos and Steak, an Istio story
This post originally appear on Marty Henderson’s personal blog A brief history In the before times of 2017, Varun Talwar and Louis Ryan sat under a forgotten waterfall at the edge of the world and […]