我如何在 Dataweave 2 中使用过滤器和映射
How can i use Filter and Mapping with Dataweave 2
我有一个输入 Json,我必须使用转换消息对其进行编辑才能输出。
我试过一起过滤和映射,但没有得到预期的结果
这里是
输入:
{
"success": true,
"identities": [
{
"system": "testSystem_A",
"type": "user_id",
"ids": [
"sys_A_Test_1",
"sys_A_Test_2"
]
},
{
"system": "testSystem_B",
"type": "account_id",
"ids": [
"sys_B_Test_1",
"sys_B_Test_2",
"sys_B_Test_3",
"sys_B_Test_4"
]
},
{
"system": "testSystem_C",
"type": "pass_id",
"ids": [
"sys_C_Test_1",
"sys_C_Test_2",
"sys_C_Test_3"
]
},
{
"system": "testSystem_D",
"type": "mock_id",
"ids": [
"sys_D_Test_1",
"sys_D_Test_2"
]
}
]
}
这是预期的结果
输出:
{
"success": true,
"identities": {
"testSystemA": [
{
"type": "user_id",
"Guid": "sys_A_Test_1"
},
{
"type": "user_id",
"Guid": "sys_A_Test_2"
}
],
"testSystemB": [
{
"type": "account_id",
"id": "sys_B_Test_1"
},
{
"type": "account_id",
"id": "sys_B_Test_2"
},
{
"type": "account_id",
"id": "sys_B_Test_3"
},
{
"type": "account_id",
"id": "sys_B_Test_4"
}
],
"testSystemC": [
{
"type": "pass_id",
"id": "sys_C_Test_1"
},
{
"type": "pass_id",
"id": "sys_C_Test_2"
},
{
"type": "pass_id",
"id": "sys_C_Test_3"
}
],
"testSystemD": [
{
"type": "mock_id",
"id": "sys_D_Test_1"
},
{
"type": "mock_id",
"id": "sys_D_Test_2"
}
]
}
}
我试过了,但完全没用,
我可以正确地为每个 id 创建映射
%dw 2.0
output application/json
---
{
success: payload.success,
identities: {
testSystem_A: (payload.identities filter ($.system =="testSystem_A") map( identity , indexOfIdentity ) -> {
"type": $."type",
"Guid": $."ids"
}),
"testSystem_B": (payload.identities filter ($.system =="testSystem_B") map( identity , indexOfIdentity ) -> {
"type": identity."type",
"id": identity."ids"
}),
testSystem_C: (payload.identities filter ($.system =="testSystem_c") map( identity , indexOfIdentity ) -> {
($."ids") map ->(id , indexOfIdentity ) -> {
"type": identity."type",
"id": identity."id"
}),
testSystem_D: (payload.identities filter ($.system =="testSystem_D") map( identity , indexOfIdentity ) -> {
"type": identity."type",
"id": identity."ids"
})
}
}
我已经在感谢所有帮助我的人了
此 DW 代码生成您想要的输出但未使用过滤器(我不知道这是否对您有帮助)
%dw 2.0
output application/json
fun isUserID(typeId: String): String = if (typeId == "user_id") "Guid" else "id"
---
{
"success": payload.success,
"identities": {(
payload.identities map ((item1) -> {
(item1.system): item1.ids map ((item2) -> {
"type": item1."type",
(isUserID(item1."type")) : item2
})
})
)}
}
我有一个输入 Json,我必须使用转换消息对其进行编辑才能输出。 我试过一起过滤和映射,但没有得到预期的结果 这里是 输入:
{
"success": true,
"identities": [
{
"system": "testSystem_A",
"type": "user_id",
"ids": [
"sys_A_Test_1",
"sys_A_Test_2"
]
},
{
"system": "testSystem_B",
"type": "account_id",
"ids": [
"sys_B_Test_1",
"sys_B_Test_2",
"sys_B_Test_3",
"sys_B_Test_4"
]
},
{
"system": "testSystem_C",
"type": "pass_id",
"ids": [
"sys_C_Test_1",
"sys_C_Test_2",
"sys_C_Test_3"
]
},
{
"system": "testSystem_D",
"type": "mock_id",
"ids": [
"sys_D_Test_1",
"sys_D_Test_2"
]
}
]
}
这是预期的结果
输出:
{
"success": true,
"identities": {
"testSystemA": [
{
"type": "user_id",
"Guid": "sys_A_Test_1"
},
{
"type": "user_id",
"Guid": "sys_A_Test_2"
}
],
"testSystemB": [
{
"type": "account_id",
"id": "sys_B_Test_1"
},
{
"type": "account_id",
"id": "sys_B_Test_2"
},
{
"type": "account_id",
"id": "sys_B_Test_3"
},
{
"type": "account_id",
"id": "sys_B_Test_4"
}
],
"testSystemC": [
{
"type": "pass_id",
"id": "sys_C_Test_1"
},
{
"type": "pass_id",
"id": "sys_C_Test_2"
},
{
"type": "pass_id",
"id": "sys_C_Test_3"
}
],
"testSystemD": [
{
"type": "mock_id",
"id": "sys_D_Test_1"
},
{
"type": "mock_id",
"id": "sys_D_Test_2"
}
]
}
}
我试过了,但完全没用,
我可以正确地为每个 id 创建映射
%dw 2.0
output application/json
---
{
success: payload.success,
identities: {
testSystem_A: (payload.identities filter ($.system =="testSystem_A") map( identity , indexOfIdentity ) -> {
"type": $."type",
"Guid": $."ids"
}),
"testSystem_B": (payload.identities filter ($.system =="testSystem_B") map( identity , indexOfIdentity ) -> {
"type": identity."type",
"id": identity."ids"
}),
testSystem_C: (payload.identities filter ($.system =="testSystem_c") map( identity , indexOfIdentity ) -> {
($."ids") map ->(id , indexOfIdentity ) -> {
"type": identity."type",
"id": identity."id"
}),
testSystem_D: (payload.identities filter ($.system =="testSystem_D") map( identity , indexOfIdentity ) -> {
"type": identity."type",
"id": identity."ids"
})
}
}
我已经在感谢所有帮助我的人了
此 DW 代码生成您想要的输出但未使用过滤器(我不知道这是否对您有帮助)
%dw 2.0
output application/json
fun isUserID(typeId: String): String = if (typeId == "user_id") "Guid" else "id"
---
{
"success": payload.success,
"identities": {(
payload.identities map ((item1) -> {
(item1.system): item1.ids map ((item2) -> {
"type": item1."type",
(isUserID(item1."type")) : item2
})
})
)}
}