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;
 
 
    @Inject
    public LookupTransform(Object spec) {
        this.spec = spec;
    }
 
    @Override
    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.

Input
{
  "personId": "12345"
}
Output
{
  "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.

Related Blog Posts
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]
Building Better Data Visualization Experiences: Part 1 of 2
Through direct experience with data scientists, business analysts, lab technicians, as well as other UX professionals, I have found that we need a better understanding of the people who will be using our data visualization products in order to build them. Creating a product utilizing data with the goal of providing insight is fundamentally different from a typical user-centric web experience, although traditional UX process methods can help.
Kafka Schema Evolution With Java Spring Boot and Protobuf
In this blog I will be demonstrating Kafka schema evolution with Java, Spring Boot and Protobuf.  This app is for tutorial purposes, so there will be instances where a refactor could happen. I tried to […]
Redis Bitmaps: Storing state in small places
Redis is a popular open source in-memory data store that supports all kinds of abstract data structures. In this post and in an accompanying example Java project, I am going to explore two great use […]