使用dataweave重命名和删除同一数组中的属性

Renaming and Deleting attributes in same array using dataweave

我的示例输入负载如下:

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

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

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

我是 运行 Mule 3.9,正在使用 dataweave 1.0

试试这个——它包含评论:

%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 未嵌套在您的数据结构中。

这应该允许您将 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)

此版本不断迭代,它将到达树的末尾。