【问题标题】:How can i use Filter and Mapping with Dataweave 2我如何在 Dataweave 2 中使用过滤器和映射
【发布时间】:2019-05-06 21:10:40
【问题描述】:

我有一个输入 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"
        })
    }
}

我已经感谢所有帮助我的人

【问题讨论】:

    标签: dataweave mule4


    【解决方案1】:

    此 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
                })
            })
        )}
    }
    

    【讨论】:

    • 谢谢,我找到了另一种扁平化的解决方案,而且代码更多。你的代码要好得多。
    猜你喜欢
    • 1970-01-01
    • 2019-08-26
    • 2019-09-04
    • 2015-11-20
    • 2021-10-27
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多