Maven Dependency to Add to your pom file
<dependency>
<groupId>com.bazaarvoice.jolt</groupId>
<artifactId>jolt-core</artifactId>
<version>${latest.jolt.version}</version>
</dependency>
<dependency>
<groupId>com.bazaarvoice.jolt</groupId>
<artifactId>json-utils</artifactId>
<version>${latest.jolt.version}</version>
</dependency>
Where latest.jolt.version
looks like 0.0.16
, and can be found by looking at the project's releases.
The two maven artifacts are:
jolt-core
: only one dependency on apache.commons for StringUtils- The goal is for the
jolt-core
artifact to be pure Java, so that it does not cause any dependency issues.
- The goal is for the
json-utils
: Jackson wrapper and testing utilities. Used by jolt-core as a test dependency.- If you are willing to pull in Jackson 2, this artifact provides nice utility methods.
- Copy-paste this code and sample data.
- Get it to run
- Replace the input and spec file with your own
Available here.
package com.bazaarvoice.jolt.sample;
import com.bazaarvoice.jolt.Chainr;
import com.bazaarvoice.jolt.JsonUtils;
import java.io.IOException;
import java.util.List;
public class JoltSample {
public static void main(String[] args) throws IOException {
// How to access the test artifacts, i.e. JSON files
// JsonUtils.classpathToList : assumes you put the test artifacts in your class path
// JsonUtils.filepathToList : you can use an absolute path to specify the files
List chainrSpecJSON = JsonUtils.classpathToList( "/json/sample/spec.json" );
Chainr chainr = Chainr.fromSpec( chainrSpecJSON );
Object inputJSON = JsonUtils.classpathToObject( "/json/sample/input.json" );
Object transformedOutput = chainr.transform( inputJSON );
System.out.println( JsonUtils.toJsonString( transformedOutput ) );
}
}
Available here.
{
"rating": {
"primary": {
"value": 3
},
"quality": {
"value": 3
}
}
}
Available here.
[
{
"operation": "shift",
"spec": {
"rating": {
"primary": {
"value": "Rating"
},
"*": {
"value": "SecondaryRatings.&1.Value",
"$": "SecondaryRatings.&.Id"
}
}
}
},
{
"operation": "default",
"spec": {
"Range" : 5,
"SecondaryRatings" : {
"*" : {
"Range" : 5
}
}
}
}
]
With pretty formatting, looks like:
{
"Rating": 3,
"Range": 5,
"SecondaryRatings": {
"quality": {
"Id": "quality",
"Value": 3,
"Range": 5
}
}
}