使用 dataweave 2.0 根据对象数组内的值进行过滤

问题描述 投票:0回答:2

下面是我输入的 JSON

{
"context": "context",
"meetings": [
    {
        "id": 123,
        "subject": "subject 123",
        "attendees": [
            {
                "type": "required",
                "emailAddress": {
                    "name": "user1",
                    "address": "[email protected]"
                }
            },
            {
                "type": "required",
                "emailAddress": {
                    "name": "!user2",
                    "address": "[email protected]"
                }
            }
        ]
    },
    {
        "id": 456,
        "subject": "subject 456",
        "attendees": [
            {
                "type": "required",
                "emailAddress": {
                    "name": "user3",
                    "address": "[email protected]"
                }
            },
            {
                "type": "required",
                "emailAddress": {
                    "name": "user4",
                    "address": "[email protected]"
                }
            }
        ]
    }
]
}

从上面的JSON中,首先,我想过滤掉用户名以

!
开头的对象,在每个结果对象中,我想添加一个新属性
email
,其中
email
是电子邮件名字以
!
开头的与会者。以下是我的预期输出:

[
{
"id": 123,
"subject": "subject 123",
"attendees": [
            {
                "type": "required",
                "emailAddress": {
                    "name": "user1",
                    "address": "[email protected]"
                }
            },
            {
                "type": "required",
                "emailAddress": {
                    "name": "!user2",
                    "address": "[email protected]"
                }
            }
        ],
"email" : "[email protected]"
}
]

我对dataweave完全陌生。有人可以帮我获得预期的输出吗?

我尝试使用以下代码

%dw 2.0
output application/json
---
payload.meetings filter ((item, index) -> item.attendees.emailAddress.name startsWith  "!") map (
{
  "id" : $.id,
  "subject" : $.subject,
  "attendees" : $.attendees,
  "email" : ($.attendees filter $.emailAddress.name startsWith  "!")[0].emailAddress.address
})

但结果在下面error :

You called the function 'startsWith' with these arguments: 
  1: Array (["user1", "user2"])
  2: String ("!")

But it expects arguments of these types:
  1: String
  2: String

4| payload.meetings filter ((item, index) -> item.attendees.emailAddress.name startsWith  "!") map (
mule dataweave mulesoft mule4
2个回答
2
投票

由于“attendees”是一个数组,使用filter来提取名字以“!”开头的地址在每个元素上并使用 map 映射出名称字段。

输入:

{
  "context": "context",
  "meetings": [
    {
      "id": 123,
      "subject": "subject 123",
      "attendees": [
        {
          "type": "required",
          "emailAddress": {
            "name": "!user1",
            "address": "[email protected]"
          }
        },
        {
          "type": "required",
          "emailAddress": {
            "name": "!user2",
            "address": "[email protected]"
          }
        }
      ]
    },
    {
      "id": 456,
      "subject": "subject 456",
      "attendees": [
        {
          "type": "required",
          "emailAddress": {
            "name": "user3",
            "address": "[email protected]"
          }
        },
        {
          "type": "required",
          "emailAddress": {
            "name": "user4",
            "address": "[email protected]"
          }
        }
      ]
    }
  ]
}

DW:

%dw 2.0
output application/json  
---
payload.meetings map ((item, index) -> {
  id: item.id,
  subject: item.subject,
  attendees: item.attendees,
  // Using do block storing the output of the emailAddress fields where name field startsWith "!"
  (do {
    var data = item.attendees.emailAddress filter ($.name startsWith "!")
    ---
       // map email field to the address from output of data stored above , if none present no email field will be in output
      (email: data.address) if (!isEmpty(data))

  })
})

输出:

[
  {
    "id": 123,
    "subject": "subject 123",
    "attendees": [
      {
        "type": "required",
        "emailAddress": {
          "name": "!user1",
          "address": "[email protected]"
        }
      },
      {
        "type": "required",
        "emailAddress": {
          "name": "!user2",
          "address": "[email protected]"
        }
      }
    ],
    "email": [
      "[email protected]",
      "[email protected]"
    ]
  },
  {
    "id": 456,
    "subject": "subject 456",
    "attendees": [
      {
        "type": "required",
        "emailAddress": {
          "name": "user3",
          "address": "[email protected]"
        }
      },
      {
        "type": "required",
        "emailAddress": {
          "name": "user4",
          "address": "[email protected]"
        }
      }
    ]
  }
]

0
投票

我想这个脚本会对你有所帮助。

输入

{
"context": "context",
"meetings": [
    {
        "id": 123,
        "subject": "subject 123",
        "attendees": [
            {
                "type": "required",
                "emailAddress": {
                    "name": "user1",
                    "address": "[email protected]"
                }
            },
            {
                "type": "required",
                "emailAddress": {
                    "name": "!user2",
                    "address": "[email protected]"
                }
            }
        ]
    },
    {
        "id": 456,
        "subject": "subject 456",
        "attendees": [
            {
                "type": "required",
                "emailAddress": {
                    "name": "user3",
                    "address": "[email protected]"
                }
            },
            {
                "type": "required",
                "emailAddress": {
                    "name": "user4",
                    "address": "[email protected]"
                }
            }
        ]
    }
]
}

代码

%dw 2.0
import * from dw::util::Values
var filteredInput = (payload update ["meetings", "attendees"] 
                        with ($ map (obj, index) -> 
                            if (obj.emailAddress.name startsWith "!") obj
                            else "NoMatch"))
output application/json 
---
filteredInput.meetings map {
  Id: $.id,
  subject: $.subject,
  attendees: ($.attendees filter $ != "NoMatch") map {
    "type": $."type",
    emailAddress: $.emailAddress
  },
  email: ($.attendees filter $ != "NoMatch").emailAddress.address
} filter not isEmpty($.attendees)

输出

[
  {
    "Id": 123,
    "subject": "subject 123",
    "attendees": [
      {
        "type": "required",
        "emailAddress": {
          "name": "!user2",
          "address": "[email protected]"
        }
      }
    ],
    "email": [
      "[email protected]"
    ]
  }
]

建议“email”字段应该是一个array,因为如果多个“name”包含“”字符,它应该显示为emails的数组。 如果您需要电子邮件作为字符串,您可以使用 joinBy() 函数。

© www.soinside.com 2019 - 2024. All rights reserved.