英文:
Filtering based on a value inside array of object using DataWeave 2.0
问题
I see that you're working with DataWeave to filter and manipulate JSON data. It looks like you're trying to achieve the expected output by filtering and adding the "email" attribute to objects where the name of the attendee starts with '!'.
Here's the corrected DataWeave code to achieve your desired result:
%dw 2.0
output application/json
---
payload.meetings filter ((item) -> item.attendees filter $.emailAddress.name startsWith "!")
map {
"id": $.id,
"subject": $.subject,
"attendees": $.attendees,
"email": $.attendees filter $.emailAddress.name startsWith "!" map $.emailAddress.address
}
The error you encountered was because you were trying to use startsWith
on an array of names, while it expects a string. In the corrected code, we filter the meetings based on attendees whose names start with '!', and then map the necessary attributes.
This code should provide the expected output as described in your original request.
英文:
Below is my input JSON
{
"context": "context",
"meetings": [
{
"id": 123,
"subject": "subject 123",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "user1",
"address": "user1@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "!user2",
"address": "user2@gmail.com"
}
}
]
},
{
"id": 456,
"subject": "subject 456",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "user3",
"address": "user3@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "user4",
"address": "user4@gmail.com"
}
}
]
}
]
}
From the above JSON, First, I want to filter out the objects where name of the user starts with !
and in each resultant object, I want to add a new attribute email
where email
is the email of the attendee whose name starts with !
. Below is my expected output:
[
{
"id": 123,
"subject": "subject 123",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "user1",
"address": "user1@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "!user2",
"address": "user2@gmail.com"
}
}
],
"email" : "user2@gmail.com"
}
]
I am completely new to DataWeave. can someone please help me in getting the expected output.
I tried with the below code
%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
})
But it turned out to be below 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 (
答案1
得分: 2
由于“attendees”是一个数组,使用筛选器来提取那些名称以“!”开头的地址,并使用映射来映射出名称字段。
输入:
{
"context": "context",
"meetings": [
{
"id": 123,
"subject": "subject 123",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "!user1",
"address": "user1@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "!user2",
"address": "user2@gmail.com"
}
}
]
},
{
"id": 456,
"subject": "subject 456",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "user3",
"address": "user3@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "user4",
"address": "user4@gmail.com"
}
}
]
}
]
}
DW:
%dw 2.0
output application/json
payload.meetings map ((item, index) -> {
id: item.id,
subject: item.subject,
attendees: item.attendees,
(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))
}
})
}) filter !isEmpty($.email)
// filters required Objects from final output
输出:
[
{
"id": 123,
"subject": "subject 123",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "!user1",
"address": "user1@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "!user2",
"address": "user2@gmail.com"
}
}
],
"email": [
"user1@gmail.com",
"user2@gmail.com"
]
},
{
"id": 456,
"subject": "subject 456",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "user3",
"address": "user3@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "user4",
"address": "user4@gmail.com"
}
}
]
}
]
英文:
Since "attendees" is an Array, use filter to extract those addresses whose names startsWith "!" on each element and using map to map out the name field.
Input:
{
"context": "context",
"meetings": [
{
"id": 123,
"subject": "subject 123",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "!user1",
"address": "user1@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "!user2",
"address": "user2@gmail.com"
}
}
]
},
{
"id": 456,
"subject": "subject 456",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "user3",
"address": "user3@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "user4",
"address": "user4@gmail.com"
}
}
]
}
]
}
DW:
%dw 2.0
output application/json
---
payload.meetings map ((item, index) -> {
id: item.id,
subject: item.subject,
attendees: item.attendees,
(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))
}
})
}) filter !isEmpty($.email)
// filters required Objects from final output
Output :
[
{
"id": 123,
"subject": "subject 123",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "!user1",
"address": "user1@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "!user2",
"address": "user2@gmail.com"
}
}
],
"email": [
"user1@gmail.com",
"user2@gmail.com"
]
},
{
"id": 456,
"subject": "subject 456",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "user3",
"address": "user3@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "user4",
"address": "user4@gmail.com"
}
}
]
}
]
答案2
得分: 1
以下是代码的翻译部分:
{
"Id": 123,
"subject": "subject 123",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "user1",
"address": "user1@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "!user2",
"address": "user2@gmail.com"
}
}
],
"email": "user2@gmail.com"
}
请注意,这是代码的翻译部分,不包括问题或其他内容。
英文:
I guess this script would help you.
Input
{
"context": "context",
"meetings": [
{
"id": 123,
"subject": "subject 123",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "user1",
"address": "user1@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "!user2",
"address": "user2@gmail.com"
}
}
]
},
{
"id": 456,
"subject": "subject 456",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "user3",
"address": "user3@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "user4",
"address": "user4@gmail.com"
}
}
]
}
]
}
Code
%dw 2.0
import * from dw::util::Values
import * from dw::core::Arrays
var filteredInput = (payload.meetings update "attendees"
with (if ($.emailAddress.name some ($ startsWith "!")) $
else null )) filter $.attendees != null
output application/json
---
filteredInput map {
Id: $.id,
subject: $.subject,
attendees: $.attendees map {
"type": $."type",
emailAddress: $.emailAddress
},
email: do{
var email = ($.attendees.emailAddress filter ($.name startsWith "!")).address
---
if(sizeOf(email) == 1) email[0] else email
}
}
Output
[
{
"Id": 123,
"subject": "subject 123",
"attendees": [
{
"type": "required",
"emailAddress": {
"name": "user1",
"address": "user1@gmail.com"
}
},
{
"type": "required",
"emailAddress": {
"name": "!user2",
"address": "user2@gmail.com"
}
}
],
"email": "user2@gmail.com"
}
]
In case only one name has the "!" character, the email will be a string, if there is more than one match, it will be an array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论