如何使用Jolt变换数组? [英] How do I transform an array using Jolt?

查看:138
本文介绍了如何使用Jolt变换数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试转换类似这样的内容时,我的转换对象得到了一个空值:

I am getting a null value for my transformed object when trying to convert something like this:

{   
  "employees": [
    { "f_name" : "tom", "l_name" : "smith" },
    { "f_name" : "don", "l_name" : "jones" }
  ]
}

对此:

{
  "employees": [
    { "firstName" : "tom", "lastName" : "smith" },
    { "firstName" : "don", "lastName" : "jones" }
  ]
}

这是我正在使用的规范:

This is the spec I am using:

[
  {
    "operation" : "shift",
    "spec" : {
      "employees" : {
        "f_name" : "firstName"
        "l_name" : "lastName"
      }
   }
 ]

这是我正在使用的代码:

This is the code I am using:

List<Object> chainrSpecJSON = JsonUtils.classpathToList("path/spec.json");
Chainr chainr = Chainr.fromSpec(chainrSpecJSON);
Object inputJSON = JsonUtils.classpathToObject("path/input.json");

Object transformed = chainr.transform(inputJSON);
System.out.println(transformed);

我能够使用与上述相同的规范和代码成功转换以下输入:

I was able to successfully transform the following input with the same spec and code as above:

{   
  "employees": 
    { "firstName" : "tom", "lastName" : "smith" }
}

那我该怎么做才能转换一组员工对象?

So what do I need to do to transform an array of employee objects?

推荐答案

此规范满足您的要求

[
  {
    "operation": "shift",
    "spec": {
      "employees": {
        "*": {
          "f_name": "employees[&1].firstName",
          "l_name": "employees[&1].lastName"
        }
      }
    }
  }
]

关键是您需要使用"*"循环遍历employees数组的所有元素,然后在递归/匹配到f_name和l_name时,需要使用[& 1引用索引数组],这意味着在树上查找两个级别(从零到一),然后将其用作输出中的索引数组.

The key thing is you need to use a "*" to loop thru all the elements of the employees array, then when you recurse / match down to f_name and l_name, you need to reference the index array using [&1], which means look up the tree two levels, zero then one, and use that as an index array in the output.

这篇关于如何使用Jolt变换数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆