2

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

Triumph Spitfire
  • 663
  • 15
  • 38

2 Answers2

3

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.

George
  • 2,758
  • 12
  • 16
1

This should allow you to rename Flag to PrimaryFlag and also add Code: [{code: null}] as a child field to 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)

This version keeps iterating and it will go to the end of your tree.

George
  • 2,758
  • 12
  • 16