Jolt (JsOn Language for Transform) is a transformation library written in Java that allows a developer to convert one JSON structure to another. Jolt provides a set of transformation types, each with their own DSL (called specifications), that define the new structure for outgoing JSON data.
Jolt allows the following transformations to generate the desired result:
shift: Copies data from input to the output tree.default: Applies default values to the tree.remove: Removes data from the tree.sort: Sorts the Map key values alphabetically.cardinality: Adjusts the cardinality of input data.modify-default-beta: Applies default values without overwriting existing values.modify-overwrite-beta: Overwrites existing values with specified defaults.
Aside from writing your own custom code to do a transform, there are two general approaches to performing a JSON to JSON transform in Java:
-
JSON -> XML -> XSLT or STX -> XML -> JSON: This is a complex and convoluted approach. XSLT is more complicated than Jolt because it tries to do the whole transform with a single DSL.
-
Write a Template (Velocity, FreeMarker, etc): This approach involves creating a template that takes hydrated JSON input and writes textual JSON output. Working from the output format backward to the input is complex for non-trivial transforms. Jolt, on the other hand, works forward from the input data to the output format, simplifying the process.
These methods were not only inefficient but also hard to maintain and scale. Jolt was created to provide a simpler, more intuitive solution that allows for declarative JSON transformations, making the process more straightforward and improving developer productivity.
Jolt was developed by Bazaarvoice, a company known for providing software that helps brands and retailers collect and display user-generated content, such as reviews and ratings, on their websites. The need for efficient and scalable data transformation solutions within their services led to the creation of Jolt, which has since been made available as an open-source library to benefit the broader developer community.
Here are some other interesting JSON manipulation tools to consider:
- jq: An awesome command line tool to extract data from JSON files (available via brew).
- JsonPath: A Java library to extract data from JSON using an XPath-like syntax.
- JsonSurfer: A Java streaming JsonPath processor dedicated to processing big and complicated JSON data.
The primary goal of Jolt is to improve developer speed by providing declarative rather than imperative transforms. Jolt should also have better runtime performance compared to some alternatives.
Optimizations include:
- Transforms can be initialized once with their spec and re-used many times in a multi-threaded environment.
- Initialized Jolt transforms can service multiple web requests from a DropWizard service.
- Optimized wildcard logic to reduce the use of Regex, resulting in significant speed improvements.
- Parallel tree walk optimization in Shiftr.
- Jolt is not stream-based, so you need enough memory to hold large JSON documents during transformation.
- The transform process creates and discards many objects, increasing garbage collector activity.
You can also try Jolt transformations online at this website: Jolt Demo.
test_jolt.java: The Java test file that executes the Jolt transformation.input.json: The input JSON file containing the data to be transformed.spec.json: The specification JSON file defining the transformation rules.
- Ensure you have Java installed on your system.
- Download the necessary Jolt libraries.
-
Compile and run
test_jolt.javato apply the transformation specified inspec.jsontoinput.json. -
You can copy the code from the
input.jsonandspec.jsonfiles and try it online at Jolt Demo.
Jolt uses several special symbols in its specifications:
*(Asterisk): Wildcard to match any key.@: References the data itself.$: Refers to the root of the input JSON tree.#: Refers to the context value in the current scope.|(Pipe): Logical OR to match multiple possible values.
Purpose: Copies data from the input to the output tree.
Example: "name": "fullName" shifts the name value to fullName in the output.
Purpose: Applies default values to the output if they do not exist.
Example: "country": "USA" adds "country": "USA" to the output if country is not present.
Purpose: Removes data from the output.
Example: "address": "" removes the address field from the output.
Purpose: Sorts the map key values alphabetically.
Example: "itemsList": "items" sorts the items array and maps it to itemsList.
Purpose: Adjusts the cardinality (number of items) of input data.
Example: "itemsList": "items" converts the items array into itemsList.
For more information about Jolt, check out the Digibee documentation.