JOLT 移位变换以过滤数组中的值 [英] JOLT shift transformation to filter values in array
问题描述
我想使用 JOLT 转换来做两件事:
- 过滤名为 myarray 的数组中的元素,以便只保留具有v_518"的元素.属性
- 过滤掉除v_518"之外的其余元素的所有属性;和lfdn"
输入:
<代码>{isError":假,isValid":真,我的阵列":[{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":空,异常消息":空,innerExceptionMessage":空}
所需的输出:
<代码>{isError":假,isValid":真,我的阵列":[{v_518":0,214506186",lfdn":89709},{v_518":0,3823236",lfdn":89710}],errorMessage":空,异常消息":空,innerExceptionMessage":空}
到目前为止我尝试过的,但没有按预期工作:
<预><代码>[{操作":移位",规格":{isError":isError",isValid":isValid",我的阵列":{//遍历值数组中的所有元素*":{v_518":{//如果值v_518";存在//抓取整个对象并将其写出//一个 v_518_array 数组.@(1,v_518)":v_518_array",@(1,lfdn)":v_518_array"}}},errorMessage":errorMessage",异常消息":异常消息",innerExceptionMessage":innerExceptionMessage";}}]我尝试使用 http://jolt-demo.appspot.com/# 中的示例andrewkcarter2 但我不知道该怎么做.
我能够解决我的问题.这个答案是我需要让球滚动的提示:https://stackoverflow.com/a/38154541/1561441
关键是通过value"引用您当前正在转换的数组.= "array[&1].value"
.
在我看来,我在这个问题上花了太多时间.有人知道 Jolt 语法的好文档吗?我自己用谷歌搜索找不到满意的.
<预><代码> [{操作":移位",规格":{isError":isError",isValid":isValid",我的阵列":{//遍历值数组中的所有元素*":{v_518":{//如果值v_518";存在//抓取整个对象并将其写出//一个 v_518_array 数组.@1":v_518_array"}}},errorMessage":errorMessage",异常消息":异常消息",innerExceptionMessage":innerExceptionMessage";}},{操作":移位",//转换数组:https://stackoverflow.com/questions/37865871/how-do-i-transform-an-array-using-jolt规格":{v_518_array":{//遍历值数组中的所有元素*":{v_518":v_518_array[&1].v_518",lfdn":v_518_array[&1].lfdn";}}}}]这里有一个更好的解决方案:
<预><代码>[{操作":移位",规格":{isError":isError",isValid":isValid",我的阵列":{//遍历值数组中的所有元素*":{v_518":{//如果值v_518";存在//抓取整个对象并将其写出//一个 v_518_array 数组.@1":v_518_array"}}},errorMessage":errorMessage",异常消息":异常消息",innerExceptionMessage":innerExceptionMessage";}},{操作":移位",//转换数组:https://stackoverflow.com/questions/37865871/how-do-i-transform-an-array-using-jolt规格":{v_518_array":{//遍历值数组中的所有元素*":{"v_518": "&2[&1].v_518",//注意这里的通用简写lfdn":&2[&1].lfdn"}}}}]I want to use a JOLT transformation to do two things:
- filter the elements in the array called myarray so that only elements remain which have a "v_518" attribute
- filter out all attributes of the remaining elements except for "v_518" and "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:
{
"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.
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
The key is referencing the array you are currently transforming via "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? 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"
}
}
}
}
]
这篇关于JOLT 移位变换以过滤数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!