简体   繁体   中英

I want to do transform json structure using jolt but iam getting error

I want to fetch the JSON transformation using a jolt

I am trying in my eclipse using maven

package com.transform;

import java.util.List;
import com.bazaarvoice.jolt.Chainr;
import com.bazaarvoice.jolt.JsonUtils;

public class transform {
    public static void main(String[] args) throws Exception {
        List<Object> specs =  
        JsonUtils.classpathToList("com.transform//src//main//java//jsoinfiles//spec.json");
        Chainr chainr = Chainr.fromSpec(specs);
        Object inputJSON =  JsonUtils.classpathToObject("com.transform//src//main//java//jsoinfiles//input.json");
        Object transformedOutput = chainr.transform(inputJSON);

        System.out.println(JsonUtils.toPrettyJsonString(transformedOutput));
    }
}

When I run the above code I get the following exception;

Exception in thread "main" java.lang.RuntimeException: Unable to load 
 JSON map from classPath : 
  com.transform//src//main//java//jsoinfiles//spec.json
at 
com.bazaarvoice.jolt.JsonUtilImpl.classpathToList(JsonUtilImpl.java:225)
at com.bazaarvoice.jolt.JsonUtils.classpathToList(JsonUtils.java:165)
at com.transform.transform.main(transform.java:9)

It's a class path issue, definitely. Your code: List specs =
JsonUtils.classpathToList("com.transform//src//main//java//jsoinfiles//spec.json"); Chainr chainr = Chainr.fromSpec(specs); Object inputJSON = JsonUtils.classpathToObject("com.transform//src//main//java//jsoinfiles//input.json"

There you are using com. transform

Remove that one. Use the below things.

//src//main//java//jsoinfiles//input.json

And //src//main//java//jsoinfiles//spec.json

Then check.

I would suggest restructuring your project and updating your code accordingly:

Structure:

./src/main/java/com/transform/App.java
./src/main/resources/jsoinfiles/spec.json
./src/main/resources/jsoinfiles/input.json

Then use the code:

package com.transform;

import java.util.List;
import com.bazaarvoice.jolt.Chainr;
import com.bazaarvoice.jolt.JsonUtils;

public class App {
    public static void main(String[] args) throws Exception {
        List<Object> specs =  
        JsonUtils.classpathToList("/jsoinfiles/spec.json");
        Chainr chainr = Chainr.fromSpec(specs);
        Object inputJSON =  JsonUtils.classpathToObject("/jsoinfiles/input.json");
        Object transformedOutput = chainr.transform(inputJSON);

        System.out.println(JsonUtils.toPrettyJsonString(transformedOutput));
    }
}

This is based on the JSONUtilsTest within the jolt project:

https://github.com/bazaarvoice/jolt/blob/master/json-utils/src/test/java/com/bazaarvoice/jolt/JsonUtilsTest.java#L131-L133

The reason behind you are getting the error is the class-path that you have mentioned in your code. use the below structure to define your class-path and you would definitely get the error resolved and the desired output.

public class App {
    public static void main(String[] args) throws Exception {
        List<Object> specs =  
        JsonUtils.classpathToList("/jsoinfiles/spec.json");
        Chainr chainr = Chainr.fromSpec(specs);
        Object inputJSON =  JsonUtils.classpathToObject("/jsoinfiles/input.json");
        Object transformedOutput = chainr.transform(inputJSON);

        System.out.println(JsonUtils.toPrettyJsonString(transformedOutput));
    }
}

Use this code for, get a file from local drive using the filepathToList and then give a path

public class Json {

@Autowired
JsonUtils jsonUtils;

public static void run()   {
    JSONObject payJSON = new JSONObject();
    String transformedJSON = null;
    Object transformedOutput = null;

    List<Object> specs = JsonUtils.filepathToList("D:\\JSON\\SPEC.json");
    Chainr chainr = Chainr.fromSpec(specs);
    Object inputJSON = JsonUtils.filepathToObject("D:\\JSON\\Inquiry_Response-Islimited-N.json");
    Object transformedOutput1 = chainr.transform(inputJSON);

    System.out.println(JsonUtils.toPrettyJsonString(transformedOutput1));

}

public static void main(String[] args) {
    run();
}

 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM