簡體   English   中英

基於嵌套值的 Dataweave 過濾數組

[英]Dataweave filtering array based on nested values

我正在嘗試根據嵌套在對象中的某些值來過濾數組。

我的數據與帳戶上有未清余額的客戶有關。 我將余額的年齡嵌套在 10-29 和 31- 的桶中。

我想保留帳戶余額超過 1000 美元的所有客戶。 我還希望保留賬戶余額超過 200 美元的客戶超過 30 天。 這些帳戶將被標記。

從示例中,客戶 50001 應該從有效負載中刪除,剩下 50002 和 50003:

{
    "data": {
        "araging": [
              {  
                "customerid": "50001",
                "aging": [
                    {
                        "agingperiod": "10-29",
                        "totalamount": "59.2"
                    },
                    {
                        "agingperiod": "31-",
                        "totalamount": "50.00"
                    }
                ]
              },
              {  
                "customerid": "50002",
                "aging": [
                    {
                        "agingperiod": "10-29",
                        "totalamount": "0.00"
                    },
                    {
                        "agingperiod": "31-",
                        "totalamount": "246.00"
                    }
                ]
              },
              {  
                "customerid": "50003",
                "aging": [
                    {
                        "agingperiod": "10-29",
                        "totalamount": "1084.60"
                    },
                    {
                        "agingperiod": "31-",
                        "totalamount": "0.00"
                    }
                ]
              }
           ] 
        }
    }
}

我的第一次嘗試是讓客戶在 30 天內超過 200 美元。

%dw 2.0
output application/json
---
{
    arAccounts: payload.data.*araging filter ($.aging.totalamount as Number > 200 and $.aging.agingperiod == "31-") map (araging, index) ->
    {
        customerid: araging.customerid,
        totalamount: araging.aging.totalamount as Number
    }
}

該代碼不斷提供有關 Arrays 無法與單個值進行比較的消息,我不清楚如何遍歷“totalamount”的每個值。 將類型從 String 更改為 Number 不起作用,因為它是一個數組值。 而且我需要知道如何對這些值求和,因為任何超過 1000 美元的帳戶都會被標記。 一個好處是在同一個變換中添加標志字段。

提前致謝。

問題是表達式$.aging.totalamount返回一個數組,而不是一個數字或字符串。 為了檢查總金額和/或賬齡期是否符合條件,一種選擇是在過濾后檢查這些數組中的每一個是否為空,正如在以下 DataWeave 表達式中所做的那樣:

%dw 2.0
output application/json
---
data: {
    araging: (payload.data.araging map 
            ($ ++ {
                flagged: !isEmpty($.aging.totalamount filter $ > 1000)
                    or (
                        !isEmpty($.aging.agingperiod filter $ == "31-")
                        and !isEmpty($.aging.totalamount filter $ > 200)
                    )
            }) 
        ) filter $.flagged
} 

使用提供的輸入有效載荷,產生的有效載荷是:

{
  "data": {
    "araging": [
      {
        "customerid": "50002",
        "aging": [
          {
            "agingperiod": "10-29",
            "totalamount": "0.00"
          },
          {
            "agingperiod": "31-",
            "totalamount": "246.00"
          }
        ],
        "flagged": true
      },
      {
        "customerid": "50003",
        "aging": [
          {
            "agingperiod": "10-29",
            "totalamount": "1084.60"
          },
          {
            "agingperiod": "31-",
            "totalamount": "0.00"
          }
        ],
        "flagged": true
      }
    ]
  }
}

如果要返回所有帳戶並使用 flagged 屬性來了解帳戶是否符合條件,則可以從上述 DataWeave 表達式中刪除filter $.flagged語句,這將導致:

{
  "data": {
    "araging": [
      {
        "customerid": "50001",
        "aging": [
          {
            "agingperiod": "10-29",
            "totalamount": "59.2"
          },
          {
            "agingperiod": "31-",
            "totalamount": "50.00"
          }
        ],
        "flagged": false
      },
      {
        "customerid": "50002",
        "aging": [
          {
            "agingperiod": "10-29",
            "totalamount": "0.00"
          },
          {
            "agingperiod": "31-",
            "totalamount": "246.00"
          }
        ],
        "flagged": true
      },
      {
        "customerid": "50003",
        "aging": [
          {
            "agingperiod": "10-29",
            "totalamount": "1084.60"
          },
          {
            "agingperiod": "31-",
            "totalamount": "0.00"
          }
        ],
        "flagged": true
      }
    ]
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM