【问题标题】:Filter an Array of Json From Mule Dataweave从 Mule Dataweave 过滤 Json 数组
【发布时间】:2019-11-22 20:19:18
【问题描述】:

我的请求 Json 如下所示。我想检索所有获得角色作为测试的 productNumber。

{
   "productRelation": [
      {
         "productNumber": 12345,
         "roles": [
            {
               "role": {
                  "code": "test",
                  "description": "test"
               }
            }
         ]
      },
      {
         "productNumber": 789,
         "roles": [
            {
               "role": {
                  "code": "dev",
                  "description": "dev"
               }
            }
         ]
      },
      {
         "productNumber": 111,
         "roles": [
            {
               "role": {
                  "code": "prod",
                  "description": "prod"
               }
            }
         ]
      }
   ]
}

我尝试使用 dataweave 过滤器将其过滤掉。我使用 mule 4 和 dataweave 2.0

【问题讨论】:

    标签: mule dataweave


    【解决方案1】:

    使用过滤器:

    %dw 2.0
    output application/json
    ---
    payload.productRelation 
        filter ((item, index) -> item.roles..code contains "test") 
        map ((item, index) -> item.productNumber)
    

    【讨论】:

      【解决方案2】:

      试试这个:

      %dw 2.0
      output application/json
      ---
      payload.productRelation reduce (e, acc=[]) -> (
          do {
              // Get the roles for the productNumber]
              var roles = e.roles..*code
              ---
              // if the roles contain test add the productNumber to the result
              if (roles contains "test") (acc + e.productNumber) else acc
          }
      )
      

      【讨论】:

      • 找到性能最好的并使用它!
      猜你喜欢
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 2021-03-26
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多