Jolt — JSON to JSON transformation library
By: Aleem Raja
Tags: Jolt, Json, Java

Problems Jolt solve:

Numerous code changes required to transform retrieved JSON into desired JSON structure.

Complex approach for JSON to JSON transform. To Transform JSON to JSON, general approaches were:

1. JSON -> XML -> XSLT or STX -> XML -> JSON

1. JSON -> XML -> XSLT or STX -> XML -> JSON 2. Write a Template (Velocity, FreeMarker, etc) that take hydrated JSON input and write textual JSON output

These approaches were complex and complicated.

Advantages:

It is an open source library.

Minimizes code changes in Java Application.

can write Java class to transform complex structure.

can write Java class to transform values.

Avoids calling getters and setters.

Where will Jolt be useful?

JSON data comes from various sources which which will be having their own format. To standardize JSON we can use Jolt. We can transform large JSON documents into desired JSON data.

Jolt Transformations:

Jolt’s JSON to JSON transformation is called Stock Transforms has its own DSL (Domain Specific Language) in order to facilitate its narrow job. all the Stock transforms just effect the “structure” of the data. If you want data transformation in JSON, you will need to write logic in Java.

To create any transformation in Jolt, instance of Chainr needs to be created. Jolt transformation’s code is as follows:

Chainr chainr = JsonUtils.classpathToList( “/path/to/chainr/specFile.json” );

Object input = JsonUtils.classpathToList( “/path/to/inputFile.json” );

Object output = chainr.transform( input );

return output;

Jolt’s stock Transformations are:

shift

default

remove

sort

cardinality

Now let’see Jolt’s stock transformations function and example.

Shift:

- Jolt’s “shift” stock transformation copies data from the input tree and put it the output tree.

Example:

Input file:

{

"employee": {

"age": {

"value": 25

},

"address": {

"value": "Pune"

}

}

}

Specification file:

[{

"operation": "shift",

"spec": {

"employee": {

"age": {

"value": "Age"

}

}

}

}]

Output file:

{

"Age": 25

}

Default:

Jolt’s “default” stock transformation applies default values to the tree.

Example:

Input file:

{

"Rating": 3,

"SecondaryRatings": {

"quality": {

"Range": 7,

"Value": 3,

"Id": "quality"

},

"sharpness": {

"Value": 4,

"Id": "sharpness"

}

}

}

Specification file:

[{

"operation": "default",

"spec": {

"RatingRange": 5,

"SecondaryRatings": {

"quality|value": {

"ValueLabel": null,

"Label": null,

"MaxLabel": "Great",

"MinLabel": "Terrible",

"DisplayType": "NORMAL"

},

"*": {

"Range": 5,

"ValueLabel": null,

"Label": null,

"MaxLabel": "High",

"MinLabel": "Low",

"DisplayType": "NORMAL"

}

}

}

}]

Output file:

{

"Rating": 3,

"SecondaryRatings": {

"quality": {

"Range": 7,

"Value": 3,

"Id": "quality",

"Label": null,

"DisplayType": "NORMAL",

"MinLabel": "Terrible",

"ValueLabel": null,

"MaxLabel": "Great"

},

"sharpness": {

"Value": 4,

"Id": "sharpness",

"DisplayType": "NORMAL",

"Range": 5,

"MinLabel": "Low",

"MaxLabel": "High",

"ValueLabel": null,

"Label": null

}

},

"RatingRange": 5

}

Remove:

Jolt’s “remove” stock transformation removes data from the input tree.

Example:

Input file:

{

"~emVersion": "2",

"id": "123124",

"productId": "31231231",

"submissionId": "34343",

"this": "stays",

"configured": {

"a": "b",

"c": "d"

}

}

Specification file:

[{

"operation": "default",

"spec": {

"~emVersion": "",

"productId": "",

"submissionId": "",

"configured": {

"c": ""

}

}

}]

Output file:

{

"~emVersion": "2",

"id": "123124",

"productId": "31231231",

"submissionId": "34343",

"this": "stays",

"configured": {

"a": "b",

"c": "d"

}

}

Sort:

Jolt’s “sort” stock transformation sorts the Map key values alphabetically ( for debugging and human readability ).

For Example:

Input file:

{

"a": "1",

"c": "3",

"b": "5",

"d": "4",

"e": {

"a": "b",

"c": "d"

}

}

Specification file:

[{

"operation": "sort",

"spec": {

"~a": "",

"c": "",

"d": "",

"f": {

"c": ""

}

}

}]

Output file:

{

"a": "1",

"b": "5",

"c": "3",

"d": "4",

"e": {

"a": "b",

"c": "d"

}

}

Cardinality:

Jolt’s “cardinality” stock transformation fixes the cardinality of input data. Eg, the “urls” element is usually a List, but if there is only one, then it is a String.

For Example:

Input file:

{

"review": {

"rating": [

5,

4

]

}

}

Specification file:

Specification file for cardinality can be constructed by specifying some rule, for example, the “rating” should be a single value.

[{

"operation": "sort",

"spec": {

"review": {

"rating": "ONE"

}

}

}]

Output file:

{

"review": {

"rating": [5, 4]

}

}

Jolt, an open-source library, is easy which makes our life easier for JSON to JSON transformation. Currently it can be used for structural transformation not data manipulation. However you can write your own Java class for the same.It provides transformations such as shift, default, remove, sort and cardinality. You can check demo and code repository in the links mentioned above.