【问题标题】:Dataweave : Need to get the desired output from the payloadDataweave :需要从有效负载中获取所需的输出
【发布时间】:2021-11-04 08:44:06
【问题描述】:

我是 MuleSoft 的新手,正在为 Data Weave 的场景苦苦挣扎。有人可以帮助我以一种优化的方式从输入有效负载中获取所需的输出:

输入:

[
  {
    "uniqueID": "1",
    "Base Rate": "0.15",
    "result": "success"
  },
  {
    "uniqueID": "1",
    "Base Rate": {
      "value": "0.15",
      "errorMessage": "Base Rate - Must be between 10 and 100"
    },
    "result": "failure"
  },
  {
    "uniqueID": "1",
    "Base Rate": {
      "value": "0.15",
      "errorMessage": "Base Rate - Cannot be a decimal"
    },
    "result": "failure"
  },
{
    "uniqueID": "1",
    "Number of Buildings": {
      "value": "1",
      "errorMessage": "Invalid Number of Buildings - Gross Area is greater than zero"
    },
    "result": "failure"
  },
   {
    "uniqueID": "2",
    "Closing Cost": {
      "value": "-1500",
      "errorMessage": "Closing Cost Cannot be negative"
    },
    "result": "failure"
  },
{
    "uniqueID": "3",
    "Base Rate Tenor": {
      "value": "",
      "errorMessage": "Base Rate Tenor - Invalid Value"
    },
    "result": "failure"
  }
]

基本利率、结算成本等字段只是示例,可以是任何字段名称;对象的数量也可以变化。 我们只需要考虑那些以“失败”为结果的对象。同一字段可以有多个错误消息,例如在输入中有 2 个不同的对象和错误消息用于“基本速率”。我们需要使用管道运算符将相同字段名称和相同唯一 ID 的错误消息组合在一起。因此,预期的输出如下:

{
    "errors":[
            {
              "uniqueId" : "1",
              "Base Rate": "BaseRate Must be between 10 and 100 | Base Rate - Cannot be a decimal"  ,
              "Number of Buildings": "Invalid Number of Buildings - Gross Area is greater than zero"
            },
            {
              "uniqueId" : "2",
              "Closing Cost": "Closing Cost Cannot be negative"         
            },
            {
              "uniqueId" : "3",
              "Base Rate Tenor": "Base Rate Tenor - Invalid Value"         
            }
        ]

【问题讨论】:

    标签: dataweave mulesoft mule4


    【解决方案1】:

    由于嵌套,它有些复杂。我必须分组,然后映射和采摘以获取预期格式的值。您可以通过将部分逻辑提取到函数中来降低复杂性,就像我在一个案例中所做的那样。

    %dw 2.0
    output application/json
    fun groupErrors(o)=o mapObject ($$): $.value joinBy  " | " 
    ---
    {
        errors: (payload filter $.result == "failure")
            groupBy ((item, index) -> item.uniqueID)
            mapObject ((value, key, index) -> 
                (key): value 
                            map (($ - "uniqueID" - "result")
                                mapObject { id: $$, value: $.errorMessage}    
                            )
            )
            pluck ((value, key, index) -> { uniqueID: key, (groupErrors(value groupBy $.id))})
    }
    

    【讨论】:

    • 感谢您的帮助;这有效
    【解决方案2】:

    请试试这个

    %dw 2.0
    output application/json
    
    fun getErrorMessage (obj)= (
        obj  mapObject (        
            (($$): $.errorMessage) if ($ is Object and (not isEmpty($.errorMessage)))
        )
    )
    
    fun formatFinalObject (obj) = (
        obj groupBy ($$) 
            mapObject ((value, key) -> 
                (key): value.*"$(key)" joinBy " | "
            )
    )
    ---
    {
        errors: payload groupBy ($.result ++ "-" ++ $.uniqueID) 
            filterObject ($$ startsWith "failure") pluck ((value, key) -> 
            {
                uniqueId: value[0].uniqueID,        
            } ++ (
                    formatFinalObject (
                        value reduce ((item, acc={}) -> 
                            acc ++ (getErrorMessage(item))
                    ))          
                )        
        )
    }
    

    【讨论】:

    • 感谢您的帮助;这有效
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 2022-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多