簡體   English   中英

Mule Dataweave - 添加嵌套和簡單屬性

[英]Mule Dataweave - Add Nested and Simple attributes

我需要使用 Dataweave (3.0) 向現有的 JSON 負載添加新屬性(簡單和嵌套)。 我在下面發布示例有效負載:

{
  "entities": [
    {
      "ID": "ABC",
      "sourceEnt": {
        "entityId": "100A",
        "entity": {
          "Code": "AB",
          "Idf1": "1pwe",
          "Idf2": null,
          "OrgAddr": [
            {
              "OrgAddrIdf1": "1pwe1",
              "Rank": 1,
              "Label": "One",
              "MainAddr": {
                "AddrLine1": "abc",
                "PoBox": 123,
                "DistrictCode": null
              }
            },
            {
              "OrgAddrIdf1": "1pwe2",
              "Rank": 2,
              "Label": "Two",
              "MainAddr": {
                "AddrLine1": "xyz",
                "PoBox": 456,
                "DistrictCode": null
              }
            }
          ]
        }
      }
    }
  ]
}

在上面的負載中,我需要向 OrgAddr.MainAddr 添加一個新屬性 ("StateCode": "null"),並在 OrgAddr 之后添加一個名為 "Flag": "Yes" 的新屬性。 我可以在最后添加新的“Flag”屬性,但如何修改嵌套屬性 (OrgAddr)。 請注意,我需要將簡單屬性和嵌套屬性添加在一起。

非常有趣的用例。 我能夠通過創建兩個允許我更新字段的輔助函數來解決它。

%dw 2.0
output application/json

/**
 * Updates the value of the specified field If the field doesn't exits it will be inserted at the bottom. Use this function with `with`. The with will receive the old value
 */
fun update(objectValue: Object, fieldName: String) = 
    (newValueProvider: (oldValue: Any, index: Number) -> Any) -> do {
        if(objectValue[fieldName]?)
            objectValue mapObject ((value, key, index) -> 
                if(key ~= fieldName)
                    {(key): newValueProvider(value, index)}
                else
                {(key): value}
            )
        else
            objectValue ++ {(fieldName): newValueProvider(null, 0)}
    }

/**
 * Updates the value at the specified index. If the index is bigger than the size it will be appended. Use this function with `with`. The with will receive the old value
 */
fun update(arrayValue: Array, indexToUpdate: Number) = 
    (newValueProvider: (oldValue: Any, index: Number) -> Any) -> do {
        if(arrayValue[indexToUpdate]?)
            arrayValue map ((value, index) -> 
                if(index == indexToUpdate)
                    newValueProvider(value, index)
                else
                    value
            )
        else
            arrayValue << newValueProvider(null, 0)
    }

---
payload update "entities" with (
    $ update 0 with (
        $ update "sourceEnt" with (
            $ update "entity" with (
                $ update "OrgAddr" with (
                        $ map ((item, index) -> item update "MainAddr" with ($ ++ {"StateCode": null}) )
                ) ++ {
                    "Flag" : "yes"
                }
            )
        )
    )
)

這兩個更新函數幫助我遍歷對象並更新樹結構中需要更新或修改的部分。

2.3 版 DataWeave 的答案

payload update {
    case entity at.entities[0].sourceEnt.entity -> do {
         entity update {
            case addresses at .OrgAddr ->  do {
            addresses map ((addr, index) -> addr  update {
                    case .MainAddr.StateCode! ->  null
            })
            }
            case .Flag! -> "Yes"
         }
    }
}

你可以試試這個:

 %dw 1.0
    %output application/json
    %var pay=flatten payload.entities.sourceEnt.entity.OrgAddr
    ---
     {(pay)} mapObject {
            ($$):$ when ($$ as :string) != "MainAddr" otherwise {"StateCode": "null"} ++ $
        }

暫無
暫無

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

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