Dataweave: 在嵌套数组中过滤数值

huangapple go评论68阅读模式
英文:

Dataweave: filtering on a value in a nested array

问题

这是您创建的数据织代码,用于过滤嵌套数组中的对象,如果一个人的兴趣爱好中有isDeleted为true的项,那么该人的对象将被移除。代码如下:

output application/json
---
(input message map ($ map object ((value, key, index) ->
if((key ~= "hobby" and (value reduce ((item, acc = []) -> acc + item.isDeleted ) contains(true))))
{"hobby": "remove"}
else
{(key):value})))
filter $.hobby != "remove"

这段代码的功能是,对输入的消息进行映射,检查每个对象的"key"和"value",如果"key"是"hobby"并且包含"item.isDeleted"为true的项,就将"hobby"设置为"remove",否则保留原始键值对。然后,使用"filter"函数删除"hobby"等于"remove"的对象。

这段代码虽然可以实现您的需求,但可能看起来比较复杂。如果您希望有更简洁的解决方案,可以考虑使用更多的数据处理函数或者将代码拆分成多个步骤以提高可读性。

英文:

I am looking for a solution to filter an object in a nested array in data weave. I have found a solution that does the job but it looks very cumbersome.

This is my input array:

var inputMessage = [
    {
    "name": "ben",
    "lastName": "TEST1",
    "hobby": [{
        id: 1,
        name: "football",
        isDeleted: false
    },
    {
        id: 2,
        name: "hockey",
        isDeleted: false
    }
    ]
    },
    {
    "name": "Bart",
    "lastName": "TEST2",
    "hobby": [{
        id: 1,
        name: "football",
        isDeleted: true
    },
    {
        id: 2,
        name: "hockey",
        isDeleted: false
    }
    ]
    }
]

This is the output:

[
  {
    "name": "ben",
    "lastName": "TEST1",
    "hobby": [
      {
        "id": 1,
        "name": "football",
        "isDeleted": false
      },
      {
        "id": 2,
        "name": "hockey",
        "isDeleted": false
      }
    ]
  }
]

As you see the person object with "Bart" is completely removed. This is done because one of the hobbies of Bart has an isDeleted: true. So when one of a person's hobbies has an is deleted: true then that person's object must be removed.

This is what I have created myself:

output application/json
---
(input message map ($ map object ((value, key, index) -> 
if((key ~= "hobby" and (value reduce ((item, acc = []) -> acc + item.isDeleted ) contains(true)))) 
{"hobby": "remove"} 
else 
{(key):value}))) 
filter $.hobby != "remove"

Anybody has an idea or solution for an easier more gentle way to solve this problem?

答案1

得分: 1

以下是您要翻译的内容:

Try as below. I hope this helps.

Input:

    [
      {
        "name": "ben",
        "lastName": "TEST1",
        "hobby": [
          {
            "id": 1,
            "name": "football",
            "isDeleted": false
          },
          {
            "id": 2,
            "name": "hockey",
            "isDeleted": false
          }
        ]
      },
      {
        "name": "Bart",
        "lastName": "TEST2",
        "hobby": [
          {
            "id": 1,
            "name": "football",
            "isDeleted": true
          },
          {
            "id": 2,
            "name": "hockey",
            "isDeleted": false
          }
        ]
      }
    ]

DataWeave Script1:

    %dw 2.0
    output application/json
    ---
    payload filter ($.hobby filter $.isDeleted == true) == [] map {
        name: $.name,
        lastName: $.lastName,
        hobby: $.hobby
    }

Dataweave Script2:

    %dw 2.0
    output application/json
    ---
    payload filter ((item, index) -> not ((item.hobby filter $.isDeleted == true) != [])) map {
        name: $.name,
        lastName: $.lastName,
        hobby: $.hobby filter $.isDeleted == false
    } filter $.hobby != "remove"

Output:

    [
      {
        "name": "ben",
        "lastName": "TEST1",
        "hobby": [
          {
            "id": 1,
            "name": "football",
            "isDeleted": false
          },
          {
            "id": 2,
            "name": "hockey",
            "isDeleted": false
          }
        ]
      }
    ]

如您所要求,这里提供了代码的翻译部分,不包括问题或回答。

英文:

Try as below. I hope this helps.

Input:

[
{
"name": "ben",
"lastName": "TEST1",
"hobby": [
{
"id": 1,
"name": "football",
"isDeleted": false
},
{
"id": 2,
"name": "hockey",
"isDeleted": false
}
]
},
{
"name": "Bart",
"lastName": "TEST2",
"hobby": [
{
"id": 1,
"name": "football",
"isDeleted": true
},
{
"id": 2,
"name": "hockey",
"isDeleted": false
}
]
}
]

DataWeave Script1:

%dw 2.0
output application/json
---
payload filter ($.hobby filter $.isDeleted == true) == [] map {
name: $.name,
lastName: $.lastName,
hobby: $.hobby
}

Dataweave Script2:

%dw 2.0
output application/json
---
payload filter ((item, index) -> not ((item.hobby filter $.isDeleted == true) != [])) map {
name: $.name,
lastName: $.lastName,
hobby: $.hobby filter $.isDeleted == false
} filter $.hobby != "remove"

Output:

[
  {
    "name": "ben",
    "lastName": "TEST1",
    "hobby": [
      {
        "id": 1,
        "name": "football",
        "isDeleted": false
      },
      {
        "id": 2,
        "name": "hockey",
        "isDeleted": false
      }
    ]
  }
]

huangapple
  • 本文由 发表于 2023年4月4日 03:26:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定