简体   繁体   English

Jolt 在转换后返回 null,但在 http://jolt-demo.appspot.com/#inception 上同样有效

[英]Jolt returns null after transforming but same works at http://jolt-demo.appspot.com/#inception

I am trying to transform the below JSON:我正在尝试转换以下 JSON:

From:从:

{
  "id": 123,
  "name": {
    "firstName": "shiva",
    "lastName": "kumar"
  },
  "dateOfBirth": "11/09/2012",
  "emailId": "mymail@gmail.com",
  "address": {
    "addressLine1": "blr",
    "addressLine2": "KA"
  },
  "salary": 12334
}

to:到:

{
  "id": 123,
  "email": "mymail@gmail.com",
  "salary": 12334,
  "fullname": {
    "firstName": "shiva",
    "lastName": "kumar"
  },
  "fullAdress": "blr KA"
}

Code: The below code just return null .代码:下面的代码只返回null

    private JSONObject tranformRequest(String json, String specFile){
    
            URI uri = Thread.currentThread().getContextClassLoader().getResource(specFile).toURI();
            String jsonSpec = Files.readAllLines(Paths.get(uri)).stream().collect(Collectors.joining());
            JSONArray jsonArray = new JSONArray(jsonSpec);
            List<Map<String, Object>> list = new ArrayList<>();
            for(int i = 0 ; i < jsonArray.length();i++){
                list.add(jsonArray.getJSONObject(i).toMap());
            }
            Chainr chainr = Chainr.fromSpec(list);//  JsonUtils.classpathToList( "/path/to/chainr/spec.json" );
    
            Object output = chainr.transform( json );
    
            return (JSONObject) output;
        }

pom.xml

         <dependency>
            <groupId>com.bazaarvoice.jolt</groupId>
            <artifactId>jolt-core</artifactId>
            <version>0.1.7</version>
        </dependency>

[ [ 来自 jolt 转换工具在线 http://jolt-demo.appspot.com/#inception][1]

If jolt failed to convert your input JSON return null .如果 jolt 无法转换您的输入 JSON ,则返回null So it can be for jolt specification.所以它可以用于震动规范。

Let's use the demo version of the site.让我们使用该站点的演示版本。 Maybe the problem is solved.也许问题就解决了。

Please update your package with version 0.1.1 like the jolt-demo website, and use the below code.请将您的 package 更新为0.1.1版本,如 jolt -demo网站,并使用以下代码。

[
  {
    "operation": "shift",
    "spec": {
      "id": "&",
      "name": "fullname",
      "emailId": "email",
      "salary": "salary",
      "address": {
        "*": "&1"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=join(' ',@0)"
    }
  }
]

If you have any problem please say in the comment.如果您有任何问题,请在评论中说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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