【问题标题】:Renaming and Deleting attributes in same array using dataweave使用dataweave重命名和删除同一数组中的属性
【发布时间】:2020-01-21 08:48:04
【问题描述】:

我的示例输入有效负载如下:

{
  "entities": [
{
  "Id": "ab5fdd89e123",
  "target": {
    "Data": {
      "attributes": {
        "Name": [],
        "Address": [
          {
            "value": {
              "AddType": [{"value": "MAIN"}],
              "Flag": [{"value": true }]
                     }
}]}}}}]}

我需要将名为 Flag (target.Data.attributes.Address.value.Flag) 的属性替换为“PrimaryFlag”,值为 true。我还需要在名为“代码”之后添加一个新属性,其值为空。所需的输出应如下所示:

{
  "entities": [
    {
      "Id": "ab5fdd89e123",
      "target": {
        "Data": {
          "attributes": {
            "Name": [],
            "Address": [
              {
                "value": {
                  "AddType": [{"value": "MAIN"}],
                  "PrimaryFlag": [{"value": true }],
                  "Code": [{"value": null}]
                         }
}]}}}}]}

我正在运行 Mule 3.9 并且正在使用 dataweave 1.0

【问题讨论】:

    标签: arrays mule anypoint-studio dataweave mulesoft


    【解决方案1】:

    试试这个——它包含 cmets:

    %dw 1.0
    %output application/dw
    %var data = {
        "entities": [
            {
                "Id": "ab5fdd89e123",
                "target": {
                    "Data": {
                        "attributes": {
                            "Name": [],
                            "Address": [
                                {
                                    "value": {
                                        "AddType": [
                                            {
                                                "value": "MAIN"
                                            }
                                        ],
                                        "Flag": [
                                            {
                                                "value": true
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
    
    // Traverse a data structure
    // When you traverse an object:
    //   1) Get the fields as strings
    //   2) Test whether you have a field Flag in your fields
    //   3) If you do then rename Flag to PrimaryFlag and add 
    //      the Code field to the current object
    //      Traversal stops at this point
    //   4) If you don't then just traverse object and recursivelly
    //      traverse the values
    // When you traverse an array:
    //   1) Iterate over the array
    //   2) Traverse every single element in the array
    // For all other types default will execute.
    %function traverse(ds) ds match {
        :object -> using (
            fs = $ pluck $$ as :string
        ) (
            (
                $ - "Flag" ++ {PrimaryFlag: $.Flag,Code: [{code: null}]} 
                when (fs contains "Flag")
                otherwise ($ mapObject {($$): traverse($)})
            )
        ),
        :array -> $ map traverse($),
        default -> $
    }
    
    ---
    traverse(data)
    

    我不得不做出一些假设,例如 Flag 字段没有嵌套在您的数据结构中。

    【讨论】:

    • 感谢您的详细解释。我会试试这个:-)
    • 地址下可以不添加“代码”,而是作为另一个属性添加(在地址之后)?
    • attributes 的孩子?是的,可以,我今天某个时候会调整代码。
    • 谢谢@George 我把你的答案复制到DW,它说输入无效{
    • @TriumphSpitfire 我正在为您要求的模组添加另一个答案。
    【解决方案2】:

    这应该允许您将Flag 重命名为PrimaryFlag,并将Code: [{code: null}] 作为子字段添加到attributes

    %dw 1.0
    %output application/dw
    %var data = {
        "entities": [
            {
                "Id": "ab5fdd89e123",
                "target": {
                    "Data": {
                        "attributes": {
                            "Name": [],
                            "Address": [
                                {
                                    "value": {
                                        "AddType": [
                                            {
                                                "value": "MAIN"
                                            }
                                        ],
                                        "Flag": [
                                            {
                                                "value": true
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
    
    // Traverse a data structure
    // When you traverse an object:
    // 1) iterate over the fields and values using the mapObject operator
    // 2) If the field is "Flag" then rename it to PrimaryFlag
    // 3) If the field is "attributes" then add the {Code: [value: null]}
    //    as a child node otherwise add the empty object, which will leave
    //    the original object unchanged
    // When you traverse an array:
    //   1) Iterate over the array
    //   2) Traverse every single element in the array
    // For all other types default will execute.
    %function traverse(ds) ds match {
        :object -> $ mapObject {
                (
                    "PrimaryFlag" when ($$ as :string == "Flag") otherwise $$
                ): traverse($) ++ ({Code: [value: null]} when ($$ as :string == "attributes") otherwise {})
        },
        :array -> $ map traverse($),
        default -> $
    }
    
    ---
    traverse(data)
    

    这个版本不断迭代,它会走到你树的末尾。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      • 2014-08-20
      • 2011-12-08
      • 2016-11-17
      • 2019-04-03
      • 1970-01-01
      相关资源
      最近更新 更多