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 Transformpackage 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" }
{ "personId": "12345", "person": { "age": "32", "lastName": "Doe", "id": "12345", "firstName": "John" } }
You can see my example with tests here https://github.com/scottbock/Jolt