Search code examples
mulemulesoft

Modify payload field values


I have incoming payload as follow:

{
    "id": "",
    "provision": {
        "switch": "xyz",
        "port": ""
    }
}

I want to transform this payload so that id gets set to 9 and port to 80. So after the transform the payload should be

{
    "id": "9",
    "provision": {
        "switch": "xyz",
        "port": "80"
    }
}

How can I do this? I am using EE 4.2.2


Solution

  • You can make use of the update operator.

    https://docs.mulesoft.com/mule-runtime/4.3/dw-values-functions-update

    Input

    {
        "id": "",
        "provision": {
            "switch": "xyz",
            "port": ""
        }
    }
    

    Script

    %dw 2.0
    import * from dw::util::Values
    output application/json
    ---
    (payload update field("id") with 9) update ["provision",field("port")] with 80
    

    Output

    {
      "id": 9,
      "provision": {
        "switch": "xyz",
        "port": 80
      }
    }