Jolt – How to Augment JSON APIs By Using Transformations

I was recently introduced to a new library called Jolt (https://github.com/bazaarvoice/jolt).  Jolt performs JSON transformations which allows a developer to convert from one JSON structure to another JSON structure using a transformation mapping (created using JSON) and not having to write code.  This is pretty amazing for Java applications as it avoids having to call hundreds of getters and setters to move from one object model to another. This makes moving from one service’s API to an internal model or an internal model to an external format much easier.

How Jolt works

Given an input JSON string the user creates a spec file, also a JSON string, to convert the input string to a new format. The most simplified version allows a user to shift the key/value pairs around to form new structures. This can help to simplify the DTO pattern used in object orientated programming by removing the need to explicitly code the relationships.

Sample Spec Transformation File

{
  "stuff": "output.new.key"
}

The above spec would take the input key stuff and map its value to a new JSON object such that the new key is output.new.key

{
  "output": {
    "new": {
      "key": 
    }
  }
}

Utilizing the Jackson API this new output JSON converts to the new set of Java objects.

Other Features

Besides from being able to shift keys, Jolt can change existing values, and set default values, along with other common tasks. Jolt’s philosophy is to do the common 90% very well and leaves the last 10% for the developer to fill in with custom code. An example of this would be calculated fields. Additional code added would support this.

To support these actions, Jolt has two basic forms, Shiftr and Chainr. Shiftr performs just the basic shift operation while Chainr provides a stack of Command objects that act on the input JSON string. This allows a user send multiple transformations at once. It is not uncommon for certain transformations that must use two shifts.

Conclusion

You can try Jolt out here: http://jolt-demo.appspot.com/. It’s still a young library but has a lot of promise. Jolt does not do stream based processing so memory could become a concern with larger models due to the amount of objects that get created and destroyed. Overall, it’s a library I’ll be using since DTOs are a pain to deal with and anything to make them easier is a plus in my book.