Search code examples
arraysmuleanypoint-studiodataweavemulesoft

Renaming and Deleting attributes in same array using dataweave


My sample input payload is given below:

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

I need to replace the attribute called Flag (target.Data.attributes.Address.value.Flag) to "PrimaryFlag" with value as true. I also need to add a new attribute after that called "Code" with value as null. The desired output should be as below:

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

I'm running Mule 3.9 and is on dataweave 1.0


Solution

  • Try this--it contains comments:

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

    I had to make some assumptions, for example the field Flag is not nested in your your data structure.