简体   繁体   English

JOLT 移位转换以过滤数组中的值

[英]JOLT shift transformation to filter values in array

I want to use a JOLT transformation to do two things:我想使用 JOLT 转换来做两件事:

  • filter the elements in the array called myarray so that only elements remain which have a "v_518" attribute过滤名为 myarray 的数组中的元素,以便仅保留具有“v_518”属性的元素
  • filter out all attributes of the remaining elements except for "v_518" and "lfdn"过滤掉除“v_518”和“lfdn”之外的其余元素的所有属性

Input:输入:

{
  "isError": false,
  "isValid": true,
  "myarray": [
    {
      "p_0001": "1",
      "p_0002": "1",
      "p_0003": "1",
      "p_0004": "1",
      "v_518": "0,214506186",
      "lfdn": 89709
    },
    {
      "p_0001": "2",
      "p_0002": "1",
      "p_0003": "1",
      "v_518": "0,3823236",
      "lfdn": 89710
    },
    {
      "p_0001": "3",
      "p_0002": "1",
      "p_0003": "1",
      "lfdn": 89711
    }
  ],
  "errorMessage": null,
  "exceptionMessage": null,
  "innerExceptionMessage": null
}

Desired output:所需的 output:

{
  "isError": false,
  "isValid": true,
  "myarray": [
    {
      "v_518": "0,214506186",
      "lfdn": 89709
    },
    {
      "v_518": "0,3823236",
      "lfdn": 89710
    }
  ],
  "errorMessage": null,
  "exceptionMessage": null,
  "innerExceptionMessage": null
}

What I tried so far, but doesn't work as intended:到目前为止我尝试过的,但没有按预期工作:

[
  {
    "operation": "shift",
    "spec": {
      "isError": "isError",
      "isValid": "isValid",
      "myarray": {
        // loop thru all the elements in value array
        "*": {
          "v_518": {
            // if the value "v_518" exists
            // grab the whole object and write it out to
            // a v_518_array array.
            "@(1,v_518)": "v_518_array",
            "@(1,lfdn)": "v_518_array"
          }
        }
      },
      "errorMessage": "errorMessage",
      "exceptionMessage": "exceptionMessage",
      "innerExceptionMessage": "innerExceptionMessage"
    }
  }
]

I tried working with the examples in http://jolt-demo.appspot.com/#andrewkcarter2 but I couldn't figure out how to do it.我尝试使用http://jolt-demo.appspot.com/#andrewkcarter2中的示例,但我不知道该怎么做。

I was able to solve my issue.我能够解决我的问题。 This answer was the hint I needed to get the ball rolling: https://stackoverflow.com/a/38154541/1561441这个答案是我需要让球滚动的提示: https://stackoverflow.com/a/38154541/1561441

The key is referencing the array you are currently transforming via "value" = "array[&1].value" .关键是通过"value" = "array[&1].value"引用您当前正在转换的数组。

In my mind I spent way too much time on this issue.在我看来,我在这个问题上花了太多时间。 Does anyone know of a good documentation for the Jolt syntax?有人知道 Jolt 语法的好文档吗? I couldn't find a satisfactory one by googling myself.我自己用谷歌搜索找不到满意的。

  [
      {
        "operation": "shift",
        "spec": {
          "isError": "isError",
          "isValid": "isValid",
          "myarray": {
            // loop thru all the elements in value array
            "*": {
              "v_518": {
                // if the value "v_518" exists
                // grab the whole object and write it out to
                // a v_518_array array.
                "@1": "v_518_array"
              }
            }
          },
          "errorMessage": "errorMessage",
          "exceptionMessage": "exceptionMessage",
          "innerExceptionMessage": "innerExceptionMessage"
        }
      },
      {
        "operation": "shift",
        //Transform array: https://stackoverflow.com/questions/37865871/how-do-i-transform-an-array-using-jolt
        "spec": {
          "v_518_array": {
            // loop thru all the elements in value array
            "*": {
              "v_518": "v_518_array[&1].v_518",
              "lfdn": "v_518_array[&1].lfdn"
            }
          }
        }
      }
    ]

Here's a slightly better solution:这是一个稍微好一点的解决方案:

[
  {
    "operation": "shift",
    "spec": {
      "isError": "isError",
      "isValid": "isValid",
      "myarray": {
        // loop thru all the elements in value array
        "*": {
          "v_518": {
            // if the value "v_518" exists
            // grab the whole object and write it out to
            // a v_518_array array.
            "@1": "v_518_array"
          }
        }
      },
      "errorMessage": "errorMessage",
      "exceptionMessage": "exceptionMessage",
      "innerExceptionMessage": "innerExceptionMessage"
    }
  },
  {
    "operation": "shift",
    //Transform array: https://stackoverflow.com/questions/37865871/how-do-i-transform-an-array-using-jolt
    "spec": {
      "v_518_array": {
        // loop thru all the elements in value array
        "*": {
          "v_518": "&2[&1].v_518", //notice the generic shorthand here
          "lfdn": "&2[&1].lfdn"
        }
      }
    }
  }
]

Still there's a briefer, and more dynamic method to figure out your issue such as还有一种更简洁、更动态的方法来解决您的问题,例如

[
  {
    "operation": "shift",
    "spec": {
      "myarray": {
        "*": {
          "v_518": {
            "@(1,&)": "&3.[&2].&",
            "@(1,lfdn)": "&3.[&2].lfdn"
          }
        }
      },
      "*": "&"
    }
  }
]

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

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