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.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
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, […]