software-mariodiana
11/8/2016 - 8:14 PM

Illustrates how to effect a JSON transform using Jolt. In this example, the JSON files are in-memory strings.

Illustrates how to effect a JSON transform using Jolt. In this example, the JSON files are in-memory strings.

// Adapted from: https://github.com/bazaarvoice/jolt/blob/master/gettingStarted.md

// https://mvnrepository.com/artifact/com.bazaarvoice.jolt/json-utils
// https://mvnrepository.com/artifact/com.bazaarvoice.jolt/jolt-core

import com.bazaarvoice.jolt.Chainr;
import com.bazaarvoice.jolt.JsonUtils;

import java.util.List;

public class JoltChainrDemo {
    public static void main(String[] args) throws Exception {
        // This stylesheet performs an identity transform.
        String spec = "[{\"operation\":\"shift\",\"spec\":{\"*\":\"&0\"}}]";

        // Input JSON document.
        String json = "{\"foo\":\"bar\"}";
        
        // Setup the machine.
        List chainrSpecJSON = JsonUtils.jsonToList(spec);
        Chainr chainr = Chainr.fromSpec(chainrSpecJSON);
        Object inputJSON = JsonUtils.jsonToObject(json);

        // Do the transformation.
        Object transformedOutput = chainr.transform(inputJSON);
        
        // The output in this case will look like the input.
        System.out.println(JsonUtils.toJsonString(transformedOutput));
    }
}