我有一个输入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"
})
}
}
我已经感谢所有能帮助我的人
答案 0 :(得分:2)
此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
})
})
)}
}