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

Arrays 使用dataweave重命名和删除同一数组中的属性,arrays,mule,anypoint-studio,dataweave,mulesoft,Arrays,Mule,Anypoint Studio,Dataweave,Mulesoft,我的输入有效负载示例如下所示: { "entities": [ { "Id": "ab5fdd89e123", "target": { "Data": { "attributes": { "Name": [], "Address": [ { "value": { "AddType": [{"value": "MAIN"}], "Fla

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

{
  "entities": [
{
  "Id": "ab5fdd89e123",
  "target": {
    "Data": {
      "attributes": {
        "Name": [],
        "Address": [
          {
            "value": {
              "AddType": [{"value": "MAIN"}],
              "Flag": [{"value": true }]
                     }
}]}}}}]}
我需要将名为Flag的属性(target.Data.attributes.Address.value.Flag)替换为“PrimaryFlag”,并将值替换为true。我还需要在“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}]
作为子字段添加到
属性中

%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)

此版本将不断迭代,并将转到树的末尾。

感谢您的详细解释。我将尝试这样做:-)与其在地址下添加“代码”,不如将其添加为另一个属性(在地址之后)?属性的子级<代码>?是的,可以,我会在今天的某个时候调整代码。谢谢@George我把你的答案复制到了DW,上面写着无效输入
{
@我正在用你请求的mods添加另一个答案。